Redirect to other page according their roles

2019-04-10 11:02发布

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

4条回答
Viruses.
2楼-- · 2019-04-10 11:18

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.

查看更多
Luminary・发光体
3楼-- · 2019-04-10 11:20

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
查看更多
欢心
4楼-- · 2019-04-10 11:29

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

查看更多
成全新的幸福
5楼-- · 2019-04-10 11:39

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.

查看更多
登录 后发表回答