{ string userData = ""; string userName = ""; if (DropDownList1.SelectedIndex == 0) // admin { userData = "Admin"; // set userData userName = "Admin User Name"; } else if (DropDownList1.SelectedIndex == 1) //customer { userData = "Customer"; // set userData userName = "Customer User Name"; } // initialize FormsAuthentication FormsAuthentication.Initialize(); // create a new ticket used for authentication FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1, userName, DateTime.Now, DateTime.Now.AddMinutes(15), false, userData); // encrypt the cookie using the machine key for secure transport string encTicket = FormsAuthentication.Encrypt(authTicket); // create and add the cookies to the list for outgoing response HttpCookie faCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket); Response.Cookies.Add(faCookie); Response.Redirect("/Admin/WebForm1.aspx");}
{ if (HttpContext.Current.User != null) { if (HttpContext.Current.User.Identity.IsAuthenticated) { if (HttpContext.Current.User.Identity is FormsIdentity) { //HttpCookie cookie = HttpContext.Current.Request.Cookies["UserRole"]; FormsIdentity id = (FormsIdentity)HttpContext.Current.User.Identity; FormsAuthenticationTicket ticket = id.Ticket; // get the stored user-data, in this case it's our users' role information string userData = ticket.UserData; string[] roles = userData.Split(','); HttpContext.Current.User = new GenericPrincipal(id, roles); } } }}
<forms name="LoginCookie" loginUrl="Login.aspx" protection="None" path="/" defaultUrl="Login.aspx" timeout="30" /></authentication><authorization> <deny users="?" /></authorization>
<location path="Admin" allowOverride="true"> <system.web> <authorization> <allow roles="Admin" /> <deny users="*"/> </authorization> </system.web></location><!--for content folder allow all--><location path="Content" allowOverride="true"> <system.web> <authorization> <allow users="*" /> </authorization> </system.web></location><!--for scripts folder allow all--><location path="Scripts" allowOverride="true"> <system.web> <authorization> <allow users="*" /> </authorization> </system.web></location><!--for images folder allow all--><location path="Images" allowOverride="true"> <system.web> <authorization> <allow users="*" /> </authorization> </system.web></location><!--for login.aspx allow all--><location path="Login.aspx" allowOverride="true"> <system.web> <authorization> <allow users="*"/> </authorization> </system.web></location><!--for default.aspx allow all--><location path="Default.aspx" allowOverride="true"> <system.web> <authorization> <allow users="*"/> </authorization> </system.web></location><!--for Admin/Login.aspx allow all--><location path="Admin/Login.aspx" allowOverride="true"> <system.web> <authorization> <allow users="*"/> </authorization> </system.web></location><!--for Customer/Login.aspx allow all--><location path="Customer/Login.aspx" allowOverride="true"> <system.web> <authorization> <allow users="*"/> </authorization> </system.web></location>
{ char[] character = { '/' }; if (Request["ReturnUrl"] != null) { // Request["ReturnUrl"].ToString() -> // http://localhost:1965/Admin/Login.aspx; // http://localhost:1965/Customer/Login.aspx string[] strs = Request["ReturnUrl"].Split(character); // if the second part is Admin go to admin login if (strs[1] == "Admin") { Response.Redirect(@"/Admin/Login.aspx"); } // if the second part is Customer go to customer login else if (strs[1] == "Customer") { Response.Redirect(@"/Customer/Login.aspx"); } }}
<br /><asp:HyperLink ID="HyperLink2" runat="server" NavigateUrl="~/Customer/WebForm1.aspx">Customer</asp:HyperLink>