Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Owin Authentication.SignIn not working

I try to use owin authentication manager to authenticate users but User.Identity.IsAuthenticated is still 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)
        });
    }
}

Some part of authentication 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

The identity creates successfully but SignIn method doesn't sign in a user. What's wrong?

like image 942
Neshta Avatar asked Oct 09 '14 20:10

Neshta


People also ask

How OWIN Authentication works?

After the user provides credentials, your application code will validate the user name and password and build user claims including user's name, roles, etc. After passing claims to the Forms authentication middleware, it will convert it to an application ticket and serialize, encrypt and encode it into a ticket token.


1 Answers

It's a very stupid mistake. I have forgotten to call ConfigureAuth method.

public partial class Startup
{
    public void Configuration(IAppBuilder app)
    {
        ConfigureAuth(app); // <-- this
        app.MapSignalR();
    }
}
like image 74
Neshta Avatar answered Sep 29 '22 09:09

Neshta