c# - Owin Authentication.SignIn 不工作

标签 c# asp.net-mvc authentication asp.net-identity owin

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

启动.cs

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

启动.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)
        });
    }
}

认证 Action 的一部分:

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

身份创建成功,但 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/

相关文章:

mysql - 登录注销考勤时间

C# 在选择查询中使用函数来解密表数据

javascript - ASP.NET MVC - 使用 AJAX 渲染 PartialView?

c# - Fiddler 看不到来自 C# HttpClient() 的 API 调用

c# - 如何根据多个条件使用 Linq 查找特定项目?

c# - 对象引用未设置为 MVC 文件(图像)上传的对象实例

c# - 对 Flickr API 的签名请求签名无效(在控制台中模拟)

jquery - Ajax 回调如何意识到用户的身份验证 session 已超时?

c# - 在 ListView 中使用 ComboBox 时的问题

c# - 当我点击我的 UIButton 时 iOS 应用程序崩溃