I'm having issues with Roles.IsUserInRole. It remains false even when the authenticated user belongs to that role. Here's my code extract
private void RedirectToPage()
{
///checks the role of the logged in user and redirects to appropriate home page
bool IsSuperadmin = Roles.IsUserInRole(loginMain.UserName, "Superadmin");
bool IsAdmin = Roles.IsUserInRole(HttpContext.Current.User.Identity.Name, "Admin");
bool IsStaff = Roles.IsUserInRole(HttpContext.Current.User.Identity.Name, "Staff");
bool ss = User.IsInRole("Admin");
if (IsSuperadmin)
{
Response.Redirect("~/superadmin/default.aspx");
}
else if (IsAdmin)
{
Response.Redirect("~/admin/default.aspx");
}
else if (IsStaff)
{
Response.Redirect("~/staff/default.aspx");
}
}
For instance, IsAdmin remains false even if the logged in user belongs to the admin role. Please help. This is really slowing down my work.
private void RedirectToPage()
{
///checks the role of the logged in user and redirects to appropriate home page
if (Roles.IsUserInRole(User.Identity.Name, "superadmin")
{
Response.Redirect("~/superadmin/default.aspx");
}
else if (Roles.IsUserInRole(User.Identity.Name, "admin")
{
Response.Redirect("~/admin/default.aspx");
}
else if (Roles.IsUserInRole(User.Identity.Name, "staff")
{
Response.Redirect("~/staff/default.aspx");
}
else (Response.Redirect("~/ REDIRECT TO DESIRED LOCATION, USER NOT ASSOCIATED WITH ANY ROLE");
}
You can use _LoggedIn event to chevk user role as below:
protected void Login1_LoggedIn(object sender, EventArgs e) {
// if there is no returnUrl in the query string , we redirect based on user role
if (string.IsNullOrEmpty(Request.QueryString["ReturnUrl"]))
{
// please don't use User.IsInRole here , because it will not be populated yet at this stage.
if (Roles.IsUserInRole(Login1.UserName, "Superadmin"))
Response.Redirect("~/superadmin/default.aspx");
else if (Roles.IsUserInRole(Login1.UserName, "Admin"))
Response.Redirect("~/admin/default.aspx");
else if (Roles.IsUserInRole(Login1.UserName, "Staff"))
Response.Redirect("~/staff/default.aspx");
}
}
Alright. Thanks Rajneesh. Just a little oversight caused all the problems. I should have used The Username from the login control instead of trying to use HttpContext.Current.User.Identity.Name. Thanks for pointing that out.
Ibangajnr
Member
45 Points
95 Posts
Roles.IsUserInRole not working
Feb 28, 2013 10:53 AM|LINK
I'm having issues with Roles.IsUserInRole. It remains false even when the authenticated user belongs to that role. Here's my code extract
private void RedirectToPage() { ///checks the role of the logged in user and redirects to appropriate home page bool IsSuperadmin = Roles.IsUserInRole(loginMain.UserName, "Superadmin"); bool IsAdmin = Roles.IsUserInRole(HttpContext.Current.User.Identity.Name, "Admin"); bool IsStaff = Roles.IsUserInRole(HttpContext.Current.User.Identity.Name, "Staff"); bool ss = User.IsInRole("Admin"); if (IsSuperadmin) { Response.Redirect("~/superadmin/default.aspx"); } else if (IsAdmin) { Response.Redirect("~/admin/default.aspx"); } else if (IsStaff) { Response.Redirect("~/staff/default.aspx"); } }For instance, IsAdmin remains false even if the logged in user belongs to the admin role. Please help. This is really slowing down my work.
sukumarraju
All-Star
16971 Points
2999 Posts
Re: Roles.IsUserInRole not working
Feb 28, 2013 11:08 AM|LINK
private void RedirectToPage() { ///checks the role of the logged in user and redirects to appropriate home page if (Roles.IsUserInRole(User.Identity.Name, "superadmin") { Response.Redirect("~/superadmin/default.aspx"); } else if (Roles.IsUserInRole(User.Identity.Name, "admin") { Response.Redirect("~/admin/default.aspx"); } else if (Roles.IsUserInRole(User.Identity.Name, "staff") { Response.Redirect("~/staff/default.aspx"); } else (Response.Redirect("~/ REDIRECT TO DESIRED LOCATION, USER NOT ASSOCIATED WITH ANY ROLE"); }Application Architecture Guide 2.0
My Blog
Twitter
Rajneesh Ver...
All-Star
37140 Points
6815 Posts
Re: Roles.IsUserInRole not working
Feb 28, 2013 11:11 AM|LINK
You can use _LoggedIn event to chevk user role as below:
protected void Login1_LoggedIn(object sender, EventArgs e) { // if there is no returnUrl in the query string , we redirect based on user role if (string.IsNullOrEmpty(Request.QueryString["ReturnUrl"])) { // please don't use User.IsInRole here , because it will not be populated yet at this stage. if (Roles.IsUserInRole(Login1.UserName, "Superadmin")) Response.Redirect("~/superadmin/default.aspx"); else if (Roles.IsUserInRole(Login1.UserName, "Admin")) Response.Redirect("~/admin/default.aspx"); else if (Roles.IsUserInRole(Login1.UserName, "Staff")) Response.Redirect("~/staff/default.aspx"); } }Detailed Description: http://forums.asp.net/t/1403132.aspx
www.rajneeshverma.com
Keep Forums Clean || Use Alert Moderators.
Ibangajnr
Member
45 Points
95 Posts
Re: Roles.IsUserInRole not working
Feb 28, 2013 11:30 AM|LINK
Thanks sukumarraju, but my role names start with uppercase letters and ASP.NET is case-sensitive about it.
Ibangajnr
Member
45 Points
95 Posts
Re: Roles.IsUserInRole not working
Feb 28, 2013 11:34 AM|LINK
Still not working. My code looks perfect. I don't understand what the issue is. Thanks.
Ibangajnr
Member
45 Points
95 Posts
Re: Roles.IsUserInRole not working
Feb 28, 2013 11:40 AM|LINK
Alright. Thanks Rajneesh. Just a little oversight caused all the problems. I should have used The Username from the login control instead of trying to use HttpContext.Current.User.Identity.Name. Thanks for pointing that out.