2014-10-09 19 views
7

Tôi cố gắng sử dụng trình quản lý xác thực owin để xác thực người dùng nhưng User.Identity.IsAuthenticated vẫn sai.Owin Authentication.SignIn không hoạt động

Startup.cs

public partial class Startup 
{ 
    public void Configuration(IAppBuilder app) 
    { 
     app.MapSignalR(); 
    } 
} 

Startup.Auth.cs

public partial class Startup 
{ 
    public static Func<UserManager<ApplicationUser>> UserManagerFactory { get; set; } 

    public Startup() 
    { 
     UserManagerFactory =() => 
     { 
      var userManager = new UserManager<ApplicationUser>(new CustomUserStore()); 
      return userManager; 
     }; 
    } 

    public void ConfigureAuth(IAppBuilder app) 
    { 
     app.UseCookieAuthentication(new CookieAuthenticationOptions 
     { 
      AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie, 
      LoginPath = new PathString("/Account/Login"), 
      LogoutPath = new PathString("/Account/LogOff"), 
      ExpireTimeSpan = TimeSpan.FromDays(7) 
     }); 
    } 
} 

Một số phần của hành động xác thực:

private async Task SignInAsync(ApplicationUser user, bool isPersistent) 
    { 
     var authManager = return HttpContext.GetOwinContext().Authentication; 
     authManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie); 
     var identity = await userManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie); 
     authManager.SignIn(new AuthenticationProperties { IsPersistent = isPersistent }, identity); 
    } 

Identity value

After sign in

Danh tính tạo thành công nhưng phương thức Đăng nhập không đăng nhập người dùng. Chuyện gì vậy?

+0

Cookie có được gửi tới trình duyệt của bạn không? nó có được trình duyệt lưu giữ không? – trailmax

+0

@trailmax, không, cookie không gửi tới trình duyệt. – Neshta

Trả lời

6

Đó là một sai lầm rất ngu ngốc. Tôi đã quên gọi phương thức ConfigureAuth.

public partial class Startup 
{ 
    public void Configuration(IAppBuilder app) 
    { 
     ConfigureAuth(app); // <-- this 
     app.MapSignalR(); 
    } 
} 
Các vấn đề liên quan