由于关于此过程的文档非常含糊且令人困惑(或旧),因此我想验证我是否正确地执行了操作并且没有错过任何步骤。Forms身份验证理解context.user.identity
我想创建一个安全的登录系统,在浏览器关闭时过期。
- 在我的web.config我有以下 -
<authentication mode="Forms">
<forms loginUrl="~/Login.aspx" defaultUrl="Index.aspx" name=".ASPXFORMSAUTH" timeout="100" />
</authentication>
<authorization>
<allow users="?" />
</authorization>
<machineKey decryption="AES" validation="SHA1" validationKey.......... />
所以我有一个用户名/密码文本框登录表单,这个按钮:
<asp:Button ID="LoginButton" runat="Server" OnClick="Login_Authenticate" Text="Sign in" />
里面Login_Authenticate我做如下:
protected void Login_Authenticate(object sender, EventArgs e){
string userName = UserName.Text;
string password = Password.Text;
bool Authenticated = false;
// Here's code that makes sure that Username and Password is CORRECT
if(AuthClass.Authenticate(userName, password)){
Authenticated = true;
}
// error checking does happen here.
if (Authenticated)
{
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, userName, DateTime.Now, DateTime.Now.AddMinutes(30), rememberUserName, String.Empty, FormsAuthentication.FormsCookiePath);
string encryptedCookie = FormsAuthentication.Encrypt(ticket);
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedCookie);
cookie.Expires = DateTime.Now.AddMinutes(30);
Response.Cookies.Add(cookie);
//FormsAuthentication.RedirectFromLoginPage(userName, false);
Response.Redirect("MainPage.aspx");
}
}
---在MasterPage.master.cs中我有以下检查Page_Init( )---
if (Context.User.Identity.IsAuthenticated)
{
int userid = (int)Session["userid"];
if (userid == null)
{
userid = GetUserID(Context.User.Identity.Name);
if (userid != null)
{
Session["userid"] = userid;
}
}
}
编辑: --- Global.asax中;一些代码,我不太清楚是正确的或不知道它做什么
protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
// look if any security information exists for this request
if (HttpContext.Current.User != null)
{
// see if this user is authenticated, any authenticated cookie (ticket) exists for this user
if (HttpContext.Current.User.Identity.IsAuthenticated)
{
// see if the authentication is done using FormsAuthentication
if (HttpContext.Current.User.Identity is FormsIdentity)
{
// Get the roles stored for this request from the ticket
// get the identity of the user
FormsIdentity identity = (FormsIdentity)HttpContext.Current.User.Identity;
//Get the form authentication ticket of the user
FormsAuthenticationTicket ticket = identity.Ticket;
//Get the roles stored as UserData into ticket
string[] roles = { };
//Create general prrincipal and assign it to current request
HttpContext.Current.User = new System.Security.Principal.GenericPrincipal(identity, roles);
}
}
}
}
---从那时起,每一页上,我使用会话用户ID来收集用户信息和内容,并确保用户具有适当的身份验证和组角色权限。
这一切是否正确?或者我必须在任何地方解密任何东西?
这足以让一个安全的用户登录?还是应该不打扰表单身份验证,并找到我自己的方式来制作自己的cookie并自行管理它?
如果什么代码失败总是验证方返回布尔认证=假真; //这是确保用户名和密码是CORRECT Authenticated = true的代码;这是你在顶部..但如果代码失败它从来没有设置认证回false。 – MethodMan 2012-01-10 21:11:51
@DJKRAZE代码确实将其设置为false,如果有错误。我只是没有包含检查用户名/密码的冗长代码。 Authenticated = true只有在一切成功时才会发生。 – Dexter 2012-01-10 21:13:58
增加了一些更多的澄清代码,包括我使用的Global.asax代码。我不知道它是否工作,我很困惑。 – Dexter 2012-01-10 21:25:03