我环顾四周,找不到我需要采取的简洁步骤,以便在我的网站中实现表单身份验证。我正在使用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);
这家伙解决了我的一切。 http://www.xoc.net/works/tips/forms-authentication.asp – 2010-06-24 19:55:45