dhaval patel

dhaval patel

  • 1.7k
  • 186
  • 14k

How to Access page to multiple roles in asp.net

Sep 1 2018 2:53 AM
I have a one application in Asp.net with Linq and i already done with single role but i want to make multiple roles to single page.
 
Please review my code and please help me how to do it.
 
In Login Page i pass the roles to login user please see below code
 
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(0, txtname.Text.Trim(), DateTime.Now, DateTime.Now.AddMinutes(2880), true, data[0].RoleName, FormsAuthentication.FormsCookiePath);
 
Next i Write Code in Global.asax
 
protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{
if (HttpContext.Current.User != null)
{
if (HttpContext.Current.User.Identity.IsAuthenticated)
{
if (HttpContext.Current.User.Identity is FormsIdentity)
{
FormsIdentity id = (FormsIdentity)HttpContext.Current.User.Identity;
FormsAuthenticationTicket ticket = id.Ticket;
string userData = ticket.UserData;
string[] roles = userData.Split(',');
HttpContext.Current.User = new System.Security.Principal.GenericPrincipal(id, roles);
}
}
}
}
 
And Now in Page coding page load event i write below code
 
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
if (!this.Page.User.Identity.IsAuthenticated)
{
Response.Redirect("~/Logintest.aspx");
}
if (!this.Page.User.IsInRole("Administrator"))
{
Response.Redirect("~/Logintest.aspx");
}
}
}
 
Now I Want USe Multiple Roles To Access Particular Page in This section.
 
if (!this.Page.User.IsInRole("Administrator"))
{
Response.Redirect("~/Logintest.aspx");
}
 
Its for Asp.net not MVC.
Please help me how to do it.

Answers (5)