Tôi gặp sự cố khi nhận dạng ASP Identity để làm mới Danh tính được lưu trữ trong cookie theo yêu cầu.ASP Identity 2.0: Regenerate Identity
Trong Startup.Auth.cs
tập tin cookie được thiết lập để tái sinh như sau:
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
Provider = new CookieAuthenticationProvider
{
OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<QuizSparkUserManager, QuizSparkUser, int>(
validateInterval: TimeSpan.FromMinutes(30),
regenerateIdentityCallback: ((manager, user) => manager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie)),
getUserIdCallback: ((claimsIdentity) => int.Parse(claimsIdentity.GetUserId())))
}
});
Tuy nhiên tôi không thể làm việc ra làm thế nào để làm mới nội dung trên User.Identity
trong mã, tức là buộc phải refresh lại cookie sắc khi tôi cần nó để làm mới.
Tôi muốn có thể sử dụng tính năng gọi lại tái tạo nhận dạng theo chương trình, điều này có khả thi không?
Vấn đề của tôi cũng tương tự như thế này: How to invalidate .AspNet.ApplicationCookie after Adding user to Role using Asp.Net Identity 2?
Tuy nhiên tôi muốn làm mới chứ không phải là vô hiệu hóa cookie.
Sửa
Sau khi xem xét các câu hỏi liên quan Tôi đã cố gắng như sau (mà không xử lý lỗi đầy đủ):
IOwinContext context = Request.GetOwinContext();
QuizSparkSignInManager manager = context.Get<QuizSparkSignInManager>();
ClaimsIdentity newIdentity = manager.CreateUserIdentity(manager.UserManager.FindById(User.Identity.GetUserId<int>()));
AuthenticateResult authenticationContext =
await context.Authentication.AuthenticateAsync(DefaultAuthenticationTypes.ApplicationCookie);
if (authenticationContext != null)
{
context.Authentication.AuthenticationResponseGrant = new AuthenticationResponseGrant(
newIdentity, authenticationContext.Properties);
}
bool first2 = User.IsInRole("Turtle");
Edit2: Tuy nhiên người dùng vẫn không xuất hiện để làm mới. Trên trang tải lại họ dường như làm mới, tôi phải trong suy nghĩ này là bởi vì cookie User.Identity là một phần của yêu cầu và không thể được thay đổi trong mã?
Bạn đang cố gắng làm gì bằng cách làm mới cookie? – trailmax
Khi bạn giải quyết trong câu trả lời của mình, tôi đang cố làm mới vai trò người dùng khi người dùng được thêm vào vai trò. – Underscore
Đây là câu hỏi rất giống: http://stackoverflow.com/a/19354940/809357 – trailmax