8

Giàn giáo Visual Studio 2015 sử dụng UserManager<TUser> không thể sử dụng để tạo ClaimsIdentity. Có ai có một ví dụ làm việc về cách làm điều này?Tạo danh tính xác nhận quyền sở hữu bằng Identity 3

Các giàn giáo VS2015 ném lỗi:

public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager) 
{ 
    // Note the authenticationType must match the one 
    // defined in CookieAuthenticationOptions.AuthenticationType 
    var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie); 

    // Add custom user claims here 
    return userIdentity; 
} 

N.B .: Tôi đã thêm tính để ApplicationUser mà không mâu thuẫn với IdentyUser.

+0

Lỗi giàn giáo nào và ở đâu? Phương thức bạn đăng cho thấy rằng nó trả về 'ClaimsIdentity' – Nkosi

+0

UserManager không chứa định nghĩa cho CreateIdentityAsync hoặc DefaultAuthenticationTypes – Ungaro

+0

Để tái tạo: VS2015 tạo một dự án web ASP.NET mới với mẫu MVC 6. Trong Mô hình trong ApplicationUser.cs, thêm tham chiếu đến System.Security.Claims và Micosoft.AspNet.Identity và chèn mã ở trên vào lớp ApplicationUser. Xem các lỗi được mô tả trong phần bình luận ở trên. – Ungaro

Trả lời

10

Trình quản lý người dùng đã thay đổi trong phiên bản MVC6. Bạn sẽ cần phải sửa đổi mã của bạn ...

public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager) { 
    var authenticationType = "Put authentication type Here"; 
    var userIdentity = new ClaimsIdentity(await manager.GetClaimsAsync(this), authenticationType); 

    // Add custom user claims here 
    return userIdentity; 
} 
+0

Cảm ơn bạn, Nkosi! – Ungaro

+2

AuthenticationType: https://msdn.microsoft.com/en-us/library/system.security.claims.authenticationtypes(v=vs.110).aspx – Ungaro

+0

Tôi đã học cách thêm các lớp DAL/DTO vào một dự án mới dựa trên ASPNETCORE từ dự án trình diễn công cộng Altostratus và nhận thấy họ có hai phương thức không đồng bộ này, người ta chỉ có 'UserManager manager' và người kia có người quản lý' UserManager , string authenticationType' là cả hai đều cần thiết? Như tôi thấy trong máy của bạn, bạn chỉ cần mã hóa thông báo loại. – Edward

Các vấn đề liên quan