Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

User.Identity.IsAuthenticated always false after PasswordSignInAsync gives success

I've got a standard MVC project, with the UserManager and SignInManager objects and an AccountController, with the pre-created login and register type functionality.

I can register new users to my AspNetUsers table, but when I login I call:-

var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);

The data is coming through from the form correctly, and the result is Success, which I expected.

I then tried the following redirects:-

case SignInStatus.Success:
    //return RedirectToLocal("/admin/");
    return RedirectToAction("Index", "Admin");

but on any page, after this successful login, User.Identity.IsAuthenticated is always false, and User.Identity.Name is an empty string.

What am I doing wrong? I've done another project in the same way with the same setup in the past and I've had zero problems.

web.config

<system.web>
    <compilation debug="true" targetFramework="4.5.1" />
    <httpRuntime targetFramework="4.5.1" />
    <!--<authentication mode="Forms">
        <forms loginUrl="~/Account/Login/" timeout="1000" />
    </authentication>-->
    <authentication mode="None" />
</system.web>
<modules>
    <remove name="FormsAuthentication" />
</modules>

Can anyone suggest what I am doing wrong? It's causing major issues now.

Cheers!

like image 655
Mike Upjohn Avatar asked Apr 01 '16 21:04

Mike Upjohn


People also ask

Why is user identity IsAuthenticated false?

identity. isauthenticated is False when a user is already logged in.

How to authenticate user with Identity?

In authentication, the user or computer has to prove its identity to the server or client. Usually, authentication by a server entails the use of a user name and password. Other ways to authenticate can be through cards, retina scans, voice recognition, and fingerprints.

What is IsAuthenticated identity?

Obviously, User. Identity. IsAuthenticated checks to see if the User is authenticated while Request. IsAuthenticated checks to see if the Request is Authenticated.


1 Answers

Check to see if you have a Startup.Auth.cs file in your App_Start folder in the project.

public partial class Startup {
    public void ConfigureAuth(IAppBuilder app) {            
        // This uses cookie to store information for the signed in user
        var authOptions = new CookieAuthenticationOptions {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,                
            LoginPath = new PathString("/Account/Login"),
            LogoutPath = new PathString("/Account/Logout"),
            ExpireTimeSpan = TimeSpan.FromDays(7),
        };
        app.UseCookieAuthentication(authOptions);
    }
}

and is called from the Startup class

public partial class Startup {
    public void Configuration(IAppBuilder app) {
        // Surface Identity provider
        ConfigureAuth(app);

        //..other start up code
    }
}

Depending on the version of asp.net and identity you are using, you should take a look at this

ASP.NET Identity AuthenticationManager vs. SignInManager and cookie expiration

like image 166
Nkosi Avatar answered Nov 11 '22 10:11

Nkosi