【问题标题】:What steps do I need to take to implement forms authentication with roles?我需要采取哪些步骤来实现带有角色的表单身份验证?
【发布时间】:2010-06-22 17:35:24
【问题描述】:

我环顾四周,找不到在我的网站中实施表单身份验证所需的简明步骤。我正在使用带有 SQL Server 后端的 C# 3.5。

我的数据库中有一个 User 表和 UserRole 表。

我的应用中有 5 个目录,其中包含 aspx 页面。

管理员
常见
用户角色1
用户角色2
公开

我想要 Admin、UserRole1 和 UserRole2 上基于角色的安全性。

我的 web.config 看起来像这样......

  <system.web>
    <authentication mode="Forms">
      <forms name=".Authentication" loginUrl="UI/Common/Login.aspx" protection="All" path="/" timeout="30" />
    </authentication>
  ...
  </sytem.web>

  <location path="UI/Admin">
    <system.web>
      <authorization>
        <allow roles="Admin"/>
        <deny users="*"/>
      </authorization>
    </system.web>
  </location>

  <location path="UI/UserRole1">
    <system.web>
      <authorization>
        <allow roles="UserRole1"/>
        <deny users="*"/>
      </authorization>
    </system.web>
  </location>

  <location path="UI/UserRole2">
    <system.web>
      <authorization>
        <allow roles="UserRole2"/>
        <deny users="*"/> 
      </authorization>
    </system.web>
  </location>

我在我的 Login.aspx 页面中放置了一个登录控件,我的 Login.aspx.cs 目前看起来像这样。

protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
{
    if ((from u in db.Users where u.UserName == Login1.UserName select u).Count() == 1)
    {
        User user = (from u in db.Users where u.UserName == Login1.UserName select u).First();
        //custom Encryption class, returns true if password is correct
        if (Encryption.VerifyHash(Login1.Password, user.Salt, user.Hash))
        {
            string myRole = (from ur in user.UserRoles where ur.UserRoleID == user.UserRoleID select ur.Role).First();
            //???    
        }
        else
        {
            e.Authenticated = false;
        }
    }
    else
    {
        e.Authenticated = false;
    }
}

Annnnd 我卡住了,我不知道如何告诉我的应用程序我的用户的角色是什么。

请帮帮我:)

谢谢!

编辑:

我将身份验证事件代码更改为

        string role = (from ur in user.UserRoles where ur.UserRoleID == user.UserRoleID select ur.Role).First();
        if (!Roles.RoleExists(role))
            Roles.CreateRole(role);
        if (Roles.FindUsersInRole(role, user.UserName).Length == 0)
            Roles.AddUserToRole(user.UserName, role);
        e.Authenticated = true;
        string returnUrl = Request.QueryString["ReturnUrl"];
        if (returnUrl == null) returnUrl = "/";
        Response.Redirect(returnUrl);

但是我总是被踢回登录屏幕。

按下登录后 Fiddler 捕获的样子
302 /Web/UI/Common/Login.aspx?ReturnUrl=%2fWeb%2fUI%2fAdmin%2fDefault.aspx
302 /Web/UI/Admin/Default.aspx
200 /Web/UI/Common/Login.aspx?ReturnUrl=%2fWeb%2fUI%2fAdmin%2fDefault.aspx

编辑 2:

我认为我已启动并运行身份验证,但我随机收到连接套接字管道错误。

我的身份验证如下所示:

        FormsAuthentication.Initialize();
    if (!Roles.RoleExists(role))
        Roles.CreateRole(role);
    if (Roles.FindUsersInRole(role, user.UserName).Length == 0)
        Roles.AddUserToRole(user.UserName, role);
    e.Authenticated = true;
    FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1,
         user.UserName,
         DateTime.Now,
         DateTime.Now.AddMinutes(30), // value of time out property
         true, // Value of IsPersistent property
         string.Empty,
         FormsAuthentication.FormsCookiePath);
    string encryptedTicket = FormsAuthentication.Encrypt(ticket);
    HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
    if (ticket.IsPersistent) authCookie.Expires = ticket.Expiration;
    authCookie.Secure = false; //set to true when https is enabled
    Response.Cookies.Add(authCookie);

    FormsAuthentication.RedirectFromLoginPage(user.UserName, true);

【问题讨论】:

标签: c# .net forms-authentication


【解决方案1】:

您可以使用Roles 类方法,如下所示:

Roles.AddUserToRole(string userName, string roleName);
Roles.AddUserToRoles(string userName, string[] roleNames);
Roles.AddUsersToRole(string[] userNames, string roleName);
Roles.AddUsersToRoles(string[] userNames, string[] roleNames;

确保您是using System.Web.Security

【讨论】:

  • 嗯,我一直被踢回登录屏幕。
  • @Biff 您可以尝试使用 LoggedIn 事件而不是 Authenticate 事件。
  • 我认为我的问题是我需要以某种方式坚持用户。
【解决方案2】:

我认为您缺少的是会员资格提供者。您可以使用默认成员提供程序或添加自定义成员提供程序(这将允许您进行自己的验证和角色分配)。看看 4 个人的 this 文章。无论哪种方式,您都应该能够将会员提供程序插入您的系统并使用您已经拥有的登录控件来维护整个站点的身份验证。

【讨论】:

    【解决方案3】:

    好的,我找到了解决方案。 http://www.xoc.net/works/tips/forms-authentication.asp

    【讨论】:

      猜你喜欢
      • 2016-01-14
      • 1970-01-01
      • 2021-03-29
      • 2010-12-14
      • 2010-11-05
      • 1970-01-01
      • 2010-12-18
      • 2020-12-08
      • 2020-06-30
      相关资源
      最近更新 更多