|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionThis article demonstrates how to use Form Authentication in ASP.NET. I have written a set of classes and a small web application that uses these classes as an example. The small application features 4 forms (pages) that allow you to do the following functions: Add new user, assign roles to users, remove roles from users and manage roles. Although the classes I've written provide quite enough functions that are ready to use, for the demonstration purpose, I have limited the fields in the The Classes OverviewThere are 4 classes: The User class
The Role class
The SitePrincipal class (implements the IIPrincipal Interface)
The SiteIdentity class (implements the IIdentity Interface)
Enabling Forms AuthenticationTo enable ASP.NET Forms Authentication, your application web.config file must contain the following information: <configuration>
<system.web>
<authentication mode="Forms">
<forms name="RolesBasedAthentication"
path="/"
loginUrl="/Login.aspx"
protection="All"
timeout="30">
</forms>
</authentication>
</system.web>
</configuration>
The authentication mode is set to When Forms Authentication is enabled, each time a user requests a page, the form will attempt to look up for a cookie in the user's browser. If one is found, the user identity was kept in the cookie represented in the
Because the Creating the Login PageFor creating the login page, you simply need 2 textboxes to let the user input the email address and password, named private void Submit_Click(object sender, System.EventArgs e)
{
// call the ValidateLogin static method to
// check if the email and password are correct
// if correct the method will return a new user else return null
SitePrincipal newUser =
SitePrincipal.ValidateLogin(Email.Text, Password.Text);
if (newUser == null)
{
ErrorMessage.Text = "Login failed for " + Email.Text;
ErrorMessage.Visible = true;
}
else
{
// assign the new user to the current context user
Context.User = newUser;
// set the cookie that contains the email address
// the true value means the cookie will be set persisted
FormsAuthentication.SetAuthCookie( Email.Text, true );
// redirect the user to the home page
Response.Redirect("Default.aspx");
}
}
The code above is straightforward, first we call Authenticating User On Every RequestWhenever user requests a page, the ASP.NET Forms Authentication will automatically pick up our cookie. But we haven't replaced the current context user with our own, so we should create a public class PageBase: System.Web.UI.Page
{
public PageBase()
{
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
this.Load += new System.EventHandler(this.PageBase_Load);
}
private void PageBase_Load(object sender, System.EventArgs e)
{
if (Context.User.Identity.IsAuthenticated)
{
if (!(Context.User is SitePrincipal))
{
SitePrincipal newUser =
new SitePrincipal( Context.User.Identity.Name );
Context.User = newUser;
}
}
}
}
So now every page should derive this bass class instead of deriving the if (Context.User.Identity.IsAuthenticated)
{
string name = ((SiteIdentity)Context.User.Identity).FullName;
string email = ((SiteIdentity)Context.User.Identity).Email;
string password = ((SiteIdentity)Context.User.Identity).Password;
string userID = ((SiteIdentity)Context.User.Identity).UserID;
}
Or if you can check if the current user is in a specific role as following: if (Context.User.Identity.IsAuthenticated)
{
// if user is not in the Site Admin role,
// he/she will be redirected to the login page
if (!((SitePrincipal)Context.User).IsInRole("Site Admin"))
Response.Redirect("Login.aspx");
}
The Demo ApplicationAll the code above is the only base for using my classes to turn your application into a roles-based authentication system. How ever I have written a small demo web application that uses these classes as an example with quite enough functions like: insert/update/delete roles, assign user to roles and remove user from roles. In order to get the application up and running, you need to have SQL Sever, since I'm not using Access as a database management system. You can download the demo application and all the source code for the classes from the links at the top of this page and follow these steps to get the application up and running:
When running the application, log on with account: admin@site.com and password: admin to have full access. Hope you find this small application helpful. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||