2010-06-22 97 views
2

我环顾四周,找不到我需要采取的简洁步骤,以便在我的网站中实现表单身份验证。我正在使用C#3.5和SQL Server后端。我需要采取哪些步骤来实现具有角色的表单身份验证?

我有我的数据库中的用户表和UserRole表。

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

联系
常见
UserRole1
UserRole2
公共

我想在管理,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); 

不过,我不断收到踢回登录屏幕。

登录按下小提琴手捕获后看起来像
302 /Web/UI/Common/Login.aspx?ReturnUrl=%2fWeb%2fUI%2fAdmin%2fDefault.aspx
302 /网络/ UI /管理/默认。 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); 
+0

这家伙解决了我的一切。 http://www.xoc.net/works/tips/forms-authentication.asp – 2010-06-24 19:55:45

回答

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

+0

嗯,我一直在踢回登录屏幕。 – 2010-06-22 17:56:04

+0

@Biff你可以尝试使用LoggedIn事件而不是Authenticate事件。 – 2010-06-22 18:04:56

+0

我认为我的问题是我需要以某种方式坚持用户。 – 2010-06-22 18:23:25

0

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

相关问题