【发布时间】: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);
【问题讨论】:
-
这家伙为我解决了一切。 xoc.net/works/tips/forms-authentication.asp
标签: c# .net forms-authentication