Who Me
Who Me

Reputation: 191

Redirect to other page according their roles

i need help from u guys here. So, on my system, there are 2 roles. Admin and users. I use login control to enable them to login to the system. How can i make these two roles redirect to different page? I am using membership and form authentication. I would appreciate if you could give some help to me. Thank you :)

Upvotes: 4

Views: 10106

Answers (4)

Yohan Apriandi
Yohan Apriandi

Reputation: 71

This code works:

Try

    If Membership.ValidateUser(Login1.UserName, Login1.Password)
    Then
        If Roles.IsUserInRole(Login1.UserName, "administrasi")
        Then
            Response.Redirect("~/administrasi/Default.aspx")
            ElseIf Roles.IsUserInRole(Login1.UserName, "client")
        Then
            Response.Redirect("~/client/Default.aspx")
        Else
            Response.Redirect("~/user/Default.aspx")
        End If
    End If
Catch ex As Exception

End Try

Upvotes: 1

phurba
phurba

Reputation: 11

This will redirect the user to respective pages based on their roles.

protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
    {
        if (Membership.ValidateUser(Login1.UserName, Login1.Password))
        {
            if (Roles.IsUserInRole(Login1.UserName, "Admin"))
            {
               Response.Redirect("~/Admin/Default.aspx");
            }
            else if (Roles.IsUserInRole(Login1.UserName, "User"))
            {
               Response.Redirect("~/User/Default.aspx");
            }               
        }
    }

Thanks.

Upvotes: 1

Who Me
Who Me

Reputation: 191

I got it right now. The first thing u need to do is, go to event at the properties of the login in control, the double click at loggedIn row, the it will direct you at cs page. Then, what u need to do is

protected void Login1_LoggedIn(object sender, EventArgs e)
{
    {
             if (Roles.IsUserInRole(Login1.UserName, "Admin"))
            Response.Redirect("~/Admin/Default.aspx");
        else if (Roles.IsUserInRole(Login1.UserName, "User"))
            Response.Redirect("~/User/Default.aspx");
    }
}

Then dont forget to set the destination URL of the login control to url that u want user redirect after login

Upvotes: 3

swannee
swannee

Reputation: 3466

Handle the Login controls "OnLoggedIn" event. In this event, determine the current users role. That can be done as follows ("LoginUser" below represents your login control):

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

http://msdn.microsoft.com/en-us/library/system.web.security.roleprincipal.getroles%28v=vs.100%29.aspx

Then use Response.Redirect based on the role to send them to the correct destination.

Upvotes: 4

Related Questions