[英]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.