Tôi cần nhận Hướng dẫn UserId trực tiếp sau khi đăng nhập thành công. Các mã sau đây không làm việc:User.Identity.IsAuthenticated là sai sau khi đăng nhập thành công
if (Membership.ValidateUser(txtUsername.Value, txtPassword.Value))
{
FormsAuthentication.SignOut();
FormsAuthentication.SetAuthCookie(txtUsername.Value, true);
if (HttpContext.Current.User.Identity.IsAuthenticated)
{
// doesn't run
Guid puk = (Guid)Membership.GetUser().ProviderUserKey;
}
}
đoạn mã sau làm việc:
if (Membership.ValidateUser(txtUsername.Value, txtPassword.Value))
{
FormsAuthentication.SignOut();
FormsAuthentication.SetAuthCookie(txtUsername.Value, true);
MembershipUser user = Membership.GetUser(txtUsername.Value);
if (user != null)
{
Guid puk = (Guid)user.ProviderUserKey;
}
}
Tại sao điều này xảy ra? Có điều gì khác để làm ngoài SetAuthCookie
không?
Có cách nào để truy cập User Identity trong cùng một yêu cầu không? –
@AhmadAhmadi bạn có thể đặt thủ công gán đối tượng CustomPrincipal cho thuộc tính Người dùng của HttpContext. –
@OleksiiAza Bạn có thể cập nhật câu trả lời của mình để chỉ cho bạn cách thực hiện điều này không? – Jake