Click here to Skip to main content
15,949,686 members
Please Sign up or sign in to vote.
2.60/5 (2 votes)
See more:
I would like to redirect user to particular page depending on the role assigned to them. Since there will be more than 1 user with the same role the code I'm using is not what I need. I'm using the ASP.NET Configuration to assign roles and set up 3 different roles. Admin, Customer, Partners. Each role will have many users. After user logins depending on their role I would like the to be redirected to a particular page. Via:Admin, Customer or Partner. My question is: can anyone help me either change the code I'm using or let me know if the code I'm using will not work. I need to not look for a SPECIFIC user name to look for ANY user name that is in a role. Any help would be appreciated.

What I have tried:

C#
protected void LoginButton_Click(object sender, EventArgs e)
{
    if (Roles.IsUserInRole(LoginControl.UserName, "admin1"))
    {
        Response.Redirect("~/AdminPages/Administration.aspx");
    }

    else if (Roles.IsUserInRole(LoginControl.UserName, "customer1"))
    {
        Response.Redirect("~/Customers/Customers.aspx");
    }

    else if (Roles.IsUserInRole(LoginControl.UserName, "partner1"))
    {
           Response.Redirect("~/Partners/Partners.aspx");
    }
}
Posted
Updated 5-Jun-16 19:56pm
v2

1 solution

The problem is probably that your code is working in the login button - so the chances are that the user isn't logged in yet, as the validation isn't complete. Which means that the redirect fails as there is no logged in user when the page loads - so the system redirects back to the login page...
Don't handle the Login Click event at all - instead, handle the Login control LoggedIn event: Login.LoggedIn Event (System.Web.UI.WebControls)[^] and use the single parameter version of the IsUserInRole method: Roles.IsUserInRole Method (String) (System.Web.Security)[^]
 
Share this answer
 
Comments
Member 9320578 6-Jun-16 11:19am    
I'm trying to use the example on the Roles.IsUserInRole Method (String) (System.Web.Security)[^] link, but the word Identity is has message"Cannot resolve symbol" I'm using the Login feature from he tool box, so I don't know if that is causing the problem. Would you have an idea why 'Identity' is lite up in red?
OriginalGriff 6-Jun-16 11:26am    
Try giving it it's full name "HttpContext.User.Identity.Name" and see if that does it - it may be that you have a class called User in your own code.
Member 9320578 6-Jun-16 22:01pm    
Now User is show something wrong with it. Message says: Cannot Access non-static property 'User' in static context

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900