Vì tài liệu về quy trình này rất mơ hồ và khó hiểu (hoặc cũ), tôi muốn xác minh rằng tôi đã làm đúng và không bỏ sót bất kỳ bước nào.Hình thức hiểu bối cảnh hiểu biết.user.identity
Tôi đang cố tạo hệ thống đăng nhập an toàn, hết hạn khi đóng trình duyệt.
- trong web.config của tôi, tôi có sau -
<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.......... />
Vì vậy, tôi có một hình thức đăng nhập với tên người dùng/mật khẩu hộp và nút này:
<asp:Button ID="LoginButton" runat="Server" OnClick="Login_Authenticate" Text="Sign in" />
Bên Login_Authenticate tôi làm như sau:
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");
}
}
--- trong MasterPage.master.cs Tôi có kiểm tra sau 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;
}
}
}
EDIT: --- Global.asax; một số mã rằng tôi không hoàn toàn chắc chắn là đúng hay biết những gì nó làm
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);
}
}
}
}
--- từ đó trở đi, trên mỗi trang, tôi sử dụng userid phiên để thu thập thông tin người dùng và nội dung và đảm bảo người dùng có quyền xác thực và quyền vai trò nhóm.
Điều này có chính xác không? Hay tôi phải giải mã mọi thứ ở đâu đó?
Điều này có đủ để đăng nhập người dùng an toàn không? Hoặc tôi không nên bận tâm với việc xác thực biểu mẫu và tìm cách riêng của mình để tự tạo cookie và tự quản lý?
gì nếu mã thất bại Authenticated luôn trả về true bool Authenticated = false; // Đây là mã đảm bảo rằng Tên người dùng và Mật khẩu được xác thực là đúng; đây là những gì bạn có ở đầu .. nhưng nếu mã không nó không bao giờ đặt xác thực trở lại sai .. – MethodMan
@ DJKRAZE mã không đặt nó thành false nếu có lỗi. Tôi chỉ không bao gồm mã dài để kiểm tra tên người dùng/mật khẩu. Authenticated = true chỉ xảy ra nếu mọi thứ thành công. – Dexter
Đã thêm một số mã khác để làm rõ và bao gồm mã Global.asax tôi đang sử dụng. Tôi không biết liệu nó có hoạt động hay không và tôi khá bối rối. – Dexter