我正在尝试根据角色将用户重定向到页面 [英] I'm trying to redirect user to page depending on role

查看:67
本文介绍了我正在尝试根据角色将用户重定向到页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据分配给他们的角色将用户重定向到特定页面。由于将有多个用户具有相同的角色,因此我使用的代码不是我需要的。我正在使用ASP.NET配置来分配角色并设置3个不同的角色。管理员,客户,合作伙伴。每个角色都有很多用户。用户登录后,根据他们的角色,我希望将其重定向到特定页面。通过:管理员,客户或合作伙伴。我的问题是:任何人都可以帮我修改我正在使用的代码,或者让我知道我使用的代码是否无效。我不需要查找SPECIFIC用户名来查找角色中的任何用户名。任何帮助将不胜感激。



我尝试过:



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:

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");
    }
}

推荐答案

问题可能是您的代码在登录按钮中工作 - 所以很可能用户尚未登录,因为验证尚未完成。这意味着重定向失败,因为页面加载时没有登录用户 - 因此系统重定向回登录页面...

根本不处理登录点击事件 - 而是,处理Login控件LoggedIn事件: Login.LoggedIn事件(System.Web.UI.WebControls) [ ^ ]并使用IsUserInRole方法的单参数版本: Roles.IsUserInRole方法(字符串)(System.Web.Security) [ ^ ]
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)[^]


这篇关于我正在尝试根据角色将用户重定向到页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆