1

I want to redirect people according to their role in the asp.net membership provider. I have some code in 'OnLoggedIn' event on the login page. Here is what I tried so far, and nothing doesn't work:

The code below returns false on the .IsAuthenticated step, it says the user is not authenticated. On which step at the login page the user is authenticated, I thought the 'OnLoggedIn' event is the right place to do this.

if (HttpContext.Current.User != null)
{
    if (HttpContext.Current.User.Identity.IsAuthenticated)
    {
        if (HttpContext.Current.User.Identity is FormsIdentity)
        {
        }
    }
}

2nd thing I tried was to get all the roles for the user, but it doesn't return anything, returns empty array of strings, and I checked the database that the role is assigned to that specific user. Here is how I am trying:

string[] userRole = Roles.GetRolesForUser(LoginUser.UserName);

So, my question is how I can get the role on the login page, do I need to check that in some other event or on some other way. Thanks for your help in advance, Laziale

Greg
  • 16,540
  • 9
  • 51
  • 97
Laziale
  • 7,965
  • 46
  • 146
  • 262

3 Answers3

2

The first situation won't work because it returns the value of the Request as it came in. Logging in with FormsAuthentication basically just sets a cookie in the Response so that the next Request gets the right User.

The second situation should work though, assuming that LoginUser is the name of your login control.

Greg
  • 16,540
  • 9
  • 51
  • 97
0

if you have some predefined set of rules like (SuperAdmin,Admin,User,....) than

you can use a switch case

and according to the authentication level

you can classify/redirect the user accordingly with respective URL

ankur
  • 4,565
  • 14
  • 64
  • 100
0

Greg is totally correct. So the question is why the roles are not returned? There could be several reasons depending on how you created the roles on your rolesprovider settings in web.config.

So make sure you roles provider is pointing to the same database you are see the roles. Also check you are not using different role provider to create roles but accessing it via different one. This can happen if you have more than one roles provider in web.config.

Also check if it isn't the applicationName issue.

If that doesn't help post your web.config roles setting as well as info on how you are creating roles.

gbs
  • 7,196
  • 5
  • 43
  • 69