Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我需要采取哪些步骤来使用角色实现表单身份验证?_C#_.net_Forms Authentication - Fatal编程技术网

C# 我需要采取哪些步骤来使用角色实现表单身份验证?

C# 我需要采取哪些步骤来使用角色实现表单身份验证?,c#,.net,forms-authentication,C#,.net,Forms Authentication,我环顾四周,找不到在我的网站中实现表单身份验证所需的简明步骤。我正在使用带有SQL Server后端的C#3.5 我的数据库中有一个User表和UserRole表 我的应用程序中有5个包含aspx页面的目录 管理员 普通的 用户角色1 用户角色2 公开的 我想要基于Admin、UserRole1和UserRole2的角色安全性 我的web.config看起来像是 <system.web> <authentication mode="Forms">

我环顾四周,找不到在我的网站中实现表单身份验证所需的简明步骤。我正在使用带有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>
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?返回URL=%2fWeb%2fUI%2fAdmin%2fDefault.aspx
302/Web/UI/Admin/Default.aspx
200/Web/UI/Common/Login.aspx?返回URL=%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);

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

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

确保您使用的是System.Web.Security

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

好的,我找到了一个解决方案。


whew

Hmm,我一直被踢回登录屏幕。@b如果你可以尝试使用LoggedIn事件而不是Authenticate事件。我想我的问题是我需要以某种方式保留用户。这家伙为我解决了所有问题。
Roles.AddUserToRole(string userName, string roleName);
Roles.AddUserToRoles(string userName, string[] roleNames);
Roles.AddUsersToRole(string[] userNames, string roleName);
Roles.AddUsersToRoles(string[] userNames, string[] roleNames;