Friday, April 17, 2009

ASP.NET Bypass forms authentication or Skip Authorization for selected pages


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




Related posts:
ASP.NET Submit form on Enter Key Default submit Button
Register custom controls dlls and user controls ascx in ASP.NET
Search records in GridView with searchbox in footer and highlight results using AJAX
User validation across pages using session after login in ASP.NET
Creating OpenFileDialog in winforms to open or browse for the file in .NET windows application using C#
www.tips-fb.com
Shout it
Stumble Upon Toolbar
Submit this story to DotNetKicks vote it on WebDevVote.com add to del.icio.us saved by 0 users
Subscribe to Feeds

0 comments:

About Me

My Photo
amiT jaiN
Hi, I am amiT jaiN Software engineer working on C#.NET and ASP.NET technologies
View my complete profile

Comments

.NET Resources

Find More Articles


Subscribe To Feeds

Subscribe by E-mail

Enter your email address:

Delivered by FeedBurner


Subscribe in your favorite reader

This site is best viewed with || You may get errors in proper display of this site if using Internet explorer


C#.NET Articles and tutorials,ASP.NET Articles - blog by amiT jaiN