ASP.NET Bypass Forms Authentication Or Skip Authorization

2:03:00 am 0 Comments

ASP.NET Bypass Forms Authentication Or Skip Authorization

In my previous articles i explained how to use forms authentication in asp.net
1. C# Forms Authentication using ticket and managing user roles in asp.net

2. ASP.NET 2.0 - Forms Authentication with C# and managing folder lavel access with multiple web.config files

And i also explained about how to validate use across pages using session and redirect to login page if user is not logged in

User validation across pages using session after login in ASP.NET using C sharp

Some readers ask me how to skip or bypass forms authentication or Authorization for selected pages in asp.net or a scenario where only few pages on site needs user to log in rest can be accessed without login.

For this we need to set HttpContext.Current.SkipAuthorization property to true in global application class called Global.asax

You can write code like this

string Url = Request.RawUrl;
int count = Url.Length - 10 ;
string TestUrl = Url.Substring(count);
string SessionData = Session["Authenticate"].ToString();
if (TestUrl != "Admin.aspx")
{
HttpContext.Current.SkipAuthorization = true;
}

Here i'm checking if the page url is not Admin.aspx than skip the authentication check
You can use multiple && (and) or || (or) conditions with this



Download the sample code attached

0 comments: