Redirect to other page according their roles

2019-04-10 10:50发布

问题:

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 :)

回答1:

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.



回答2:

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



回答3:

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.



回答4:

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