Asp.Net

ProgrammingAsp.net Topics:

Top 10 Topics – Conceptual/Orientation

1. ASP.NET Page Life Cycle Overview

2. ASP.NET Web Applications

3. ASP.NET Master Pages Overview

4. ASP.NET Session State Overview

5. What’s New in ASP.NET 4 and Visual Web Developer

6. ASP.NET Cookies Overview

7. ASP.NET Web Site Paths

8. ASP.NET User Controls

9. Using ASP.NET Web Services

10. Introduction to Membership
Top 10 Types – Managed Reference

1. GridView Class (System.Web.UI.WebControls)

2. ScriptManager Class (System.Web.UI)

3. FileUpload Class (System.Web.UI.WebControls)

4. TreeView Class (System.Web.UI.WebControls)

5. Login Class (System.Web.UI.WebControls)

6. UpdatePanel Class (System.Web.UI)

7. Page Class (System.Web.UI)

8. DataGrid Class (System.Web.UI.WebControls)

9. Menu Class (System.Web.UI.WebControls)

10. ListView Class (System.Web.UI.WebControls)
Top 10 Reference – General

1. ASP.NET IIS Registration Tool (Aspnet_regiis.exe)

2. @ Page

3. ASP.NET SQL Server Registration Tool (Aspnet_regsql.exe)

4. @ OutputCache

5. Application Directives

6. Code Declaration Blocks

7. Server-Side Object Tag Syntax

8. Server-Side Include Directive Syntax

9. Directives for ASP.NET Web Pages

10. Code Render Blocks

Froms Authentication Using Cookies in Asp.net
1.To using forms authenticatioon a setting is required under web.configfile.
We have to include System.Web.Security namespace to work with Forms authentication.

mode represents authentication mode , here we are using forms authentication.
name represents cookie name where we are storing logged in user details.
loginUrl represents the first page when user launches the website. when time out cookie will be deleted and again user will be redirecting to this page.
defaultUrl represents after successful login user will be redirecting to this page.
timeout cookie expiration time.
2.We can authenticate the user for particular page also.

path represents for which page we are going to perform authentication.
allow roles represents which roles we are allowing to access.
we can use allow users to performing user based authentication.
deny users=”*” represents we denying access to remaining all the users. Here * represents remaining all users.

3.Place the below code under btnLogin_Click() event.
FormsAuthentication.RedirectFromLoginPage(txtUserName.Text, false);
FormsAuthenticationTicket tkt = new FormsAuthenticationTicket(
1
, txtUserName.Text
, DateTime.Now
, DateTime.Now.AddMinutes(1)
, false
, txtRoles.Text
);
string cookieName = FormsAuthentication.Encrypt(tkt);
HttpCookie httpcookie = new HttpCookie(FormsAuthentication.FormsCookieName, cookieName);
Response.Cookies.Add(httpcookie);
string returnUrl = Request.QueryString[“ReturnUrl”];
if (returnUrl == null)
{
returnUrl = “Welcome.aspx”;
}
Response.Redirect(returnUrl, false);

RedirectFromLoginPage() is used for redirecting to the another page after successfull login .It takes 2 parameters. First parameter represents user name and second parameter represents which type of cookie we are using to store user data. true is for persistent cookie(like remember me) and false is for InMemory cookie.
Then we need to create the token for role based authentication.
Encrypt the above ticket.
Then create cookie .
Then add cookie to the Response object.
Request.QueryString[“ReturnUrl”] is used to read last viewed url. After timeout, cookie will be expired and used will be redirecting to login page, after successful login redirection done to the last viewed url.

4.Place the below code in global.asax.cs
void Application_AuthenticateRequest(object sender, EventArgs e)
{
if (HttpContext.Current.User != null
&& HttpContext.Current.User.Identity.IsAuthenticated
&& HttpContext.Current.User.Identity is FormsIdentity)
{
FormsIdentity id = (FormsIdentity)HttpContext.Current.User.Identity;
FormsAuthenticationTicket ticket = id.Ticket;
string userInfo = ticket.UserData;
string[] roles = userInfo.Split(‘,’);
HttpContext.Current.User = new GenericPrincipal(id, roles);
}

}

check if the user is null, Is authenticated and we are using forms authentication or not.
Then read Identity.
Then read ticket.
Then read UserData.
Store user roles in a string array.
GenericPrincipal() checks all the roles and which role user can access. To use this method we have to include System.Security.Principal namespace.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: