本文介绍了ASP.NET验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的,用户在他们的用户名和密码,进入登录页面。

使用这些信息,我需要再确认,他们是管理1角色的一部分如果是这样,我喜欢来设置用户的计算机上的cookie。

随着code下面我有User.InRole它不进入if语句。如果我取消了FormsAuthentication.SetAuthCookie(txtUserName.Text,真);上面它的工作原理。
意思是我不应该设置只有在用户管理1角色的一部分,该cookie

我有以下的,但似乎并没有工作:

 如果(Membership.ValidateUser(txtUserName.Text,txtPassword.Text))
    {     // FormsAuthentication.SetAuthCookie(txtUserName.Text,真);        如果(User.IsInRole(管理1))
        {
            // code不会到达这里
            FormsAuthentication.SetAuthCookie(txtUserName.Text,真);


解决方案

User.IsInRole(管理1)是验证后右边的假,因为主要对象有不被附加到当前的HttpContext 呢。

如果你真的想使用 Context.User ,你需要手动附加主要对象。

  VAR用户名= txtUserName.Text;
VAR密码= txtPassword.Text;如果(Membership.ValidateUser(用户名,密码))
{
    VAR角色= Roles.GetRolesForUser(用户名);
    VAR身份=新的GenericIdentity(用户名);
    VAR本金=新的GenericPrincipal(身份,角色);
    Context.User =本金;    //现在你可以使用Context.User    //基本上User.IsInRole(管理1)是一样的roles.Contains(管理1)
    如果(User.IsInRole(管理1))
    {
        FormsAuthentication.SetAuthCookie(用户名,真实);
    }
}

更新 - 使用Login控件验证用户

由于您使用的成员资格提供程序和角色提供程序,我想建议使用登录控制。

一旦用户通过验证后,就可以使用的loggedIn事件重定向用户appropiate页。

 < ASP:登录ID =LoginUser=服务器的EnableViewState =假
   RenderOuterTable =假OnLoggedIn =LoginUser_LoggedIn>
   ...
< / ASP:登录>保护无效LoginUser_LoggedIn(对象发件人,EventArgs的发送)
{
   //现在我们知道,用户通过身份验证
   //会员用户= Membership.GetUser(Login1.Username);
   VAR角色= Roles.GetRolesForUser(Login1.Username);   如果(roles.Contains(管理1))
      的Response.Redirect(〜/行政/);
   其他
      的Response.Redirect(〜/用户/);
}

I have the following a login page where the user enters in their username and password.

With that info, I need to then make sure that they are part of the Admin1 role If so, I like to set a cookie on the user's machine.

With the code I have below User.InRole it doesn't enter into the if statement. If I uncomment the FormsAuthentication.SetAuthCookie(txtUserName.Text, true); above it works.Meaning shouldn't I set the cookie only if the user is part of Admin1 role

I have the following but does not seem to work:

    if (Membership.ValidateUser(txtUserName.Text, txtPassword.Text))
    {

     //   FormsAuthentication.SetAuthCookie(txtUserName.Text, true);

        if (User.IsInRole("Admin1"))
        {
            // code never reaches here
            FormsAuthentication.SetAuthCookie(txtUserName.Text, true);
解决方案

User.IsInRole("Admin1") is false right after validation, because principal object hasn't been attached to the current HttpContext yet.

If you really want to use Context.User, you need to manually attach principal object.

var username = txtUserName.Text;
var password = txtPassword.Text;

if (Membership.ValidateUser(username , password))
{
    var roles = Roles.GetRolesForUser(username);
    var identity = new GenericIdentity(username);
    var principal = new GenericPrincipal(identity, roles);
    Context.User = principal;

    // Now you can use Context.User

    // Basically User.IsInRole("Admin1") is same as roles.Contains("Admin1")
    if (User.IsInRole("Admin1"))
    {
        FormsAuthentication.SetAuthCookie(username, true);
    }
}

Updated - Authenticate user using Login Control

Since you are using Membership Provider and Role Provider, I would like to suggest to use Login Control.

Once user is authenticated, you can use LoggedIn event to redirect user to appropiate page.

<asp:Login ID="LoginUser" runat="server" EnableViewState="false"
   RenderOuterTable="false" OnLoggedIn="LoginUser_LoggedIn">
   ...
</asp:Login>

protected void LoginUser_LoggedIn(object sender, EventArgs e)
{
   // Now we know that user is authenticated
   // Membership user = Membership.GetUser(Login1.Username);
   var roles = Roles.GetRolesForUser(Login1.Username);

   if(roles.Contains("Admin1"))
      Response.Redirect("~/Admin/");
   else
      Response.Redirect("~/Users/");
}

这篇关于ASP.NET验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 13:36