【问题标题】:How to manage Roles in asp.net using FORMS如何使用 Forms 管理 asp.net 中的角色
【发布时间】:2016-02-18 03:51:30
【问题描述】:

所以我正在为我的asp.net 网站制作一个登录系统。有 3 种不同类型的用户。我发现 FORMS 可以管理角色,所以我决定尝试一下。

我目前在 FORMS 中使用身份验证的所有内容 - 但没有角色。我发现这段代码应该限制对特定页面的访问。但是每个人仍然可以访问该页面。这很奇怪,因为我没有将任何人添加到角色“成员”中。一开始我只添加了 1 个角色来查看是否有人被阻止访问该页面。

   <configuration>

    <connectionStrings>
  //EDITED
 </connectionStrings>
 <system.web>

   <roleManager enabled="true" />

   <customErrors mode ="Off">

   </customErrors>

   <authentication mode="Forms">
     <forms name=".ASPXAUTH"
            loginUrl="login.aspx"
            protection="All"
            timeout="30"
            path="/">
     </forms>
   </authentication>

   <authorization>

     <deny users="?" />
     <allow users="*" />

   </authorization>


    </system.web>

  <location path="RandomPage.aspx">
    <system.web>
      <authorization>
        <allow roles="Member" />
        <deny users="*" />
      </authorization>
    </system.web>
  </location>

  <system.webServer>


    <modules runAllManagedModulesForAllRequests="true"/>
    </system.webServer>
</configuration>

将角色添加到 FormsAuthenticationTicket 的代码。 P.Userole 包含字符串“Member”

FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
                1, //Ticket version
                p.firstName, //username
                DateTime.Now, 
                DateTime.Now.AddMinutes(30), 
                false, //true for persistant user cookie
                p.userRole+"",
                FormsAuthentication.FormsCookiePath);
            string hashCookies = FormsAuthentication.Encrypt(ticket);
            HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, hashCookies);
            Response.Cookies.Add(cookie);
            Response.Redirect("Default.aspx");

【问题讨论】:

    标签: asp.net forms roles


    【解决方案1】:

    我确定,您在成功登录后没有向FormsAuthenticationTicket 添加角色。应该是……

    FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, "UserId", 
    DateTime.Now, DateTime.Now.AddMinutes(30), false, "ListOfRolesCommandSeperate", FormsAuthentication.FormsCookiePath);
        string hashCookies = FormsAuthentication.Encrypt(ticket);
        HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, hashCookies);
        Response.Cookies.Add(cookie);
    

    您需要将登录用户的角色传递给FormsAuthenticationTicket 才能使其工作。因为您只是在 web.config 文件中添加了权限。

    【讨论】:

    • 但如果我没有添加任何角色,该人将无法访问 RandomPage。他可以,但是在票证中添加角色并不是一个坏主意:) 所以:在 UserID 之前的数字 1 是什么意思?和 CommandSeperate 意味着它必须是这样的:“Admin,User,Member”对吗? - 英语不是我的第一语言谢谢你的回答:)
    • 使用上面我已经设法添加角色,并使用 FormsIdentity id = (FormsIdentity)HttpContext.Current.User.Identity; 检索它们FormsAuthenticationTicket 票 = id.Ticket;字符串用户数据 = 票。用户数据;字符串[] 角色 = userData.Split(','); HttpContext.Current.User = new GenericPrincipal(id, 角色);但它仍然不会阻止没有 RandomPage 角色的用户。
    • ,如果您将用户限制在特定页面,那么也提供页面扩展名。
    • 如果特定文件夹中的特定页面,那么您需要提供完整路径,例如...
    • 非常感谢,现在我得到了一个错误 =D 非常好的帮助,所以它说角色管理器有问题。我把它放在 Location 标签内,实际上认为这是错误的。应该放在哪里还是可以直接移除?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-24
    • 2011-01-12
    • 2011-02-13
    相关资源
    最近更新 更多