1) Non-Form Authentication :-
For this I will create three
pages, namely Login.aspx, Default.aspx and MyProfile.aspx. I guess, you might come to know from page names what it will do. To go user desire page, I have created an session variable name Session["DesirePage"], which will store an your current URL address. And in login page, I will check if this session is null or not. if not, Then it will redirect user to its desire page. |
My Login page looks like below one :-

protected void btnSumbit_Click(object sender, EventArgs
e)
{
if
(txtUserName.Text == "manojk"
&& txtPassword.Text == "manojk")
{
Session["UserName"]
= txtUserName.Text;
if
(Session["DesirePage"] != null)
{
Response.Redirect(Convert.ToString(Session["DesirePage"]));
}
else
{
Response.Redirect("Default.aspx"); ;
}
}
}
As mention in code, I will check session variable Session[“DesirePage”]
if its null or not. after check it will go to necessary condition.
Note :- I am not doing database connect to check for user id and password, as main thing I want to explain how to go user desire page.
Note :- I am not doing database connect to check for user id and password, as main thing I want to explain how to go user desire page.
In this page, I am not doing anything special just checking if
session[“UserName”] is null or not.
protected void Page_Load(object
sender, EventArgs e)
{
if (Session["UserName"] != null)
{
Response.Write(string.Format("welcome {0}, this is default page",Session["UserName"]));
}
else
{
Response.Redirect("Login.aspx");
}
}
Note, that here Session["DesirePage"] check
its not required as its an default page.
MyProfile.aspx
protected void Page_Load(object
sender, EventArgs e)
{
if (Session["UserName"] != null)
{
Response.Write(string.Format("welcome {0}, this is your profile
page", Session["UserName"]));
}
else
{
Session["DesirePage"]
= Request.RawUrl;
Response.Redirect("Login.aspx");
}
}
As you might notice in above code that I am
assigning session[“DesirePage”] as Request.RawUrl. So, when you try to
access MyProfile.aspx, and session[“UserName”] is null it will assign
session[“DesirePage”] and then it will redirect to login page.
You can test this scenario by making MyProfile.aspx page as start up page.
You can test this scenario by making MyProfile.aspx page as start up page.
2) Using Form Authentication :-
Using form authentication, I have to
check one more step.. In form authentication, initially I have to
check if Request.QueryString["ReturnUrl"] is null or not.
protected void btnSumbit_Click(object sender, EventArgs
e)
{
if
(txtUserName.Text == "manojk"
&& txtPassword.Text == "manojk")
{
Session["UserName"] = txtUserName.Text;
HttpCookie
AuthCookie;
AuthCookie = FormsAuthentication.GetAuthCookie(txtUserName.Text,
true);
AuthCookie.Expires
= DateTime.Now.AddDays(10);
Response.Cookies.Add(AuthCookie);
if(Request.QueryString["ReturnUrl"] != null)
Response.Redirect(Request.QueryString["ReturnUrl"]);
else
{
if
(Session["DesirePage"] !=
null)
Response.Redirect(Convert.ToString(Session["DesirePage"]));
else
Response.Redirect("Default.aspx"); ;
}
}
}
No comments:
Post a Comment