ASP - Net Authentication and Authorization
ASP - Net Authentication and Authorization
Authorization in ASP.NET
Forms Authentication, Users, Roles, Membership
Svetlin Nakov
Telerik Corporation
www.telerik.com
Table of Contents
1.
Basic principles
2.
Authentication Types
Windows Authentication
Forms Authentication
3.
4.
5.
Basics
Authentication
Windows Authentication
In Windows Authentication mode the Web
use
the same:
User names
Passwords
Permissions
It is the default authentication when a new
username
the Web.config:
<authentication mode="Windows" />
To deny anonymous
<authorization>
<deny users="?"/>
</authorization>
users add:
HTTP requests:
HTTP responses:
HTTP/1.1 200 OK
<html> </html>
Windows Authentication
Live Demo
Forms Authentication
Forms Authentication uses a Web form to
Web.config file
Separate user database
Users are local
Configuring Authorization
in Web.config
Configuring Authorization
in Web.config (2)
Forms Authentication
Live Demo
application
Once a user is logged-in, a set of roles and
permissions are assigned to him
Authorization in ASP.NET is
based on users and roles
DeleteUser()
GeneratePassword()
ValidateUser()
Roles in ASP.NET
Roles in ASP.NET allow assigning
permissions
to a group of users
E.g. "Admins" role could have more privileges
than "Guests" role
A user account can be assigned
to multiple
IsUserInRole()
GetAllRoles()
GetRolesForUser()
Registering a
Membership Provider
<roleManager enabled="true"
DefaultProvider="MyRoleProvider">
<providers>
<add connectionStringName="UsersConnectionString"
name="MyRoleProvider"
type="System.Web.Security.SqlRoleProvider" />
</providers>
</roleManager>
<connectionStrings>
<add name="UsersConnectionString"
connectionString="Data Source=.\SQLEXPRESS;Initial
Catalog=Users;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
aspnet_regsql.exe
Live Demo
Implementing logout:
FormsAuthentication.SignOut();
Creating
new user:
Membership.CreateUser(username, password);
Creating
new role:
Roles.CreateRole("Admins");
role:
Roles.AddUserToRole("admin", "Admins");
Membership Provider
Live Demo
configuration
Simple interface
providers
Can manage application
configuration settings
his
be shown
to users through templates, based on their
roles
AnonymousTemplate
and LoggedInTemplate
The CreateUserWizard
Control (2)
The PasswordRecovery
Control
It is used to retrieve passwords
The user is first prompted to enter username
Once users enter valid
The password
The ChangePassword
Control
Allows
the Web.config
Can be added to any page with the following
tag:
<asp:ChangePassword id="cpChangePass" runat="server"/>
The ChangePassword
Control
Questions?
Exercises
1.
2.
3.
Exercises (2)
Login.aspx accessible to everyone
Register.aspx accessible to everyone allows
visitors to register
Main.aspx accessible to logged-in users only
Admin.aspx accessible to Administrators roles only
allows users to be listed and deleted
4.
Exercises (3)
5.
Roles(ID, Name)
6.