简体   繁体   English

登录后如何使用asp.net中的角色管理重定向用户?

[英]How to redirect user after login using role management in asp.net?

I have 4 roles in AspNetMembership Database 我在AspNetMembership数据库中有4个角色

 Admin
 User
 Dealer
 Operator

I want to 我想要

Admin users go to Admin/Admin.aspx, 管理员用户转到Admin / Admin.aspx,

User users go to User/User.aspx, 用户用户转到User / User.aspx,

Dealer users go to Dealer/Dealer.aspx, 经销商用户请访问Dealer / Dealer.aspx,

Operator users go to Operator/Operator.aspx after Login. 登录后,操作员用户将访问Operator / Operator.aspx。

How can I do ? 我能怎么做 ?

My Login.aspx.cs 我的Login.aspx.cs

  namespace MyWebApp {
  public partial class Login : System.Web.UI.Page {
    protected void Page_Load(object sender, EventArgs e) 

    {

    }

    protected void btnLogin_Click(object sender, EventArgs e) 

    {
        if (Membership.ValidateUser(tbUserName.Text, tbPassword.Text))
        {
            if(string.IsNullOrEmpty(Request.QueryString["ReturnUrl"]))

            {


                FormsAuthentication.SetAuthCookie(tbUserName.Text, false);

            }




            else
                FormsAuthentication.RedirectFromLoginPage(tbUserName.Text, false);
        }

        else {
            tbUserName.ErrorText = "Invalid user";
            tbUserName.IsValid = false;
           }
       }
   }
}

The simplest way, though probably not the ideal way, would be to perform a check during the redirect. 尽管可能不是理想的方法,但最简单的方法是在重定向期间执行检查。 I would set up your roles as constants or an enumerable somewhere, then conditionally check: 我会将您的角色设置为常量或可枚举的某个位置,然后有条件地检查:

if(string.IsNullOrEmpty(Request.QueryString["ReturnUrl"]))
{
    FormsAuthentication.SetAuthCookie(tbUserName.Text, false);

    if (HttpContext.Current.User.IsInRole(ADMIN))
    {
        Response.Redirect(ADMIN_URL, true);
    }
    else if (HttpContext.Current.User.IsInRole(USER))
    {
        Response.Redirect(USER_URL, true);
    }
    else if (HttpContext.Current.User.IsInRole(DEALER))
    {
        Response.Redirect(DEALER_URL, true);
    }
    else if (HttpContext.Current.User.IsInRole(OPERATOR))
    {
        Response.Redirect(OPERATOR_URL, true);
    }
    else
    {
        Response.Redirect(SOME_DEFAULT_URL, true);
    }
}
else
{
    FormsAuthentication.RedirectFromLoginPage(tbUserName.Text, false);
}

Conceivably this check would be abstracted into a method in a library or even later within this class. 可以想象,该检查将被抽象到库中的方法中,或者甚至稍后在此类中。 This method would perform a redirect based on the first encountered role, so if a user had multiple roles (eg DEALER/OPERATOR) the first would take precedence. 此方法将根据遇到的第一个角色执行重定向,因此,如果用户有多个角色(例如,经销商/操作员),则第一个优先。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 登录后,用户根据asp.net中的角色重定向其他页面 - After Login User redirect different Page according to Role In asp.net asp.net中的用户管理角色 - User management role in asp.net 如何根据用户在Asp.net C#中的角色将用户从登录页面重定向到下一页 - how to redirect user from login page to next page base on their Role in Asp.net C# ASP.NET 核心 - 如何在登录时使用用户名获取特定用户的角色 - ASP.NET Core - How to get the Role of a particular user using his username at the point of Login 使用ASP.NET登录后如何获取用户名和角色? (将aspnetdb集成到我的数据库中) - How to get username and role after login using ASP.NET? (integrated aspnetdb into my database) 根据ASP.NET MVC 4 Internet应用程序登录中的用户角色重定向到其他页面 - redirect to different pages based on user role in ASP.NET MVC 4 Internet Application Login Asp.net Identity 2.0-根据登录时的角色重定向外部认证的用户 - Asp.net Identity 2.0 - redirect externally authenticated user based on role at login 在asp.net中限制用户后如何将用户重定向到登录页面 - how to redirect user to login page after restricting them in asp.net 在ASP.net Identity中,如何在登录后将用户重定向到特定的URL? - In ASP.net Identity, how do you redirect a user to a specific url after login? 如何在asp.net mvc 3登录后将用户重定向回到他所在的位置 - How to redirect a user back to where he was after login in asp.net mvc 3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM