Page Authentication in C#


Friday, September 4, 2009

Hi friends,

Suppose if somebody browse some internal page of website, which require login authentication. Then mostly what happen is, the page is redirected to login page. After successfully login, user has to repeat whole cycle to reach that internal page; this is time consuming and frustrating for the visitor of the site.

The code, I am providing below, using that user don’t need to repeat whole cycle to reach that internal page, after login, User will be automatically redirected to that internal page of the website.

Code in C#:

If you are using Master page, then you should use this below code.

Put this below code in code behind of Master Page on Page_Load event

Explanation:

On login page, you might be creating Session for MemberId, suppose in below sample code, I have created Session [“AdminMemberID”] on login page on click event of submit button.

So I am just using that Session variable to compare for login authentication on master page. if user is not login and if you will try to open some internal page (Required Authentication) then below code will save that internal link in Session[“URL”] and redirect you to login page.

if (Session["AdminMemberID"] == null)

{

Session["URL"] = Request.Url.AbsoluteUri;

Response.Redirect("login.aspx");

}

                                      

[Note]: If you are not using master page then you need to keep above code to each .aspx page which requires user authentication.

Put below code on click event of Login button or submit button, which ever on your login page.

if (Session["URL"] == null)

{

Response.Redirect("Myaccount.aspx");

}

else

{

string URL = Convert.ToString(Session["URL"]);

Session.Remove("URL");

Response.Redirect(URL);

}




Hope this post will help you,
if yes please put comment below of this page,
Rajesh Singh,
Asp.Net Developer
Indianic Infotech Ltd (India)
rajesh@indianic.com

No comments :