User.Identity.IsAuthenticated always false after P

2019-06-16 19:40发布

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!

2条回答
地球回转人心会变
2楼-- · 2019-06-16 20:13

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

查看更多
SAY GOODBYE
3楼-- · 2019-06-16 20:27

For me it was the web.config, comment following lines

<system.webServer>
    <modules>
      <!--<remove name="FormsAuthentication" />
      <remove name="ApplicationInsightsWebTracking" />
      <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" preCondition="managedHandler" />
      -->
    </modules>
</system.webServer>
查看更多
登录 后发表回答