我尝试使用owin身份验证管理器对用户进行身份验证,但User.Identity.IsAuthenticated仍然为false。

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)
        });
    }
}

身份验证操作的一部分:
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);
    }



身份创建成功,但SignIn方法未登录用户。怎么了?

最佳答案

这是一个非常愚蠢的错误。我忘记了调用ConfigureAuth方法。

public partial class Startup
{
    public void Configuration(IAppBuilder app)
    {
        ConfigureAuth(app); // <-- this
        app.MapSignalR();
    }
}

关于c# - Owin Authentication.SignIn不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26287634/

10-08 20:45