User.Identity.IsAuthenticated is always false after PasswordSignInAsync gives success

I have a standard MVC project with UserManager and SignInManager and AccountController objects with pre-created functionality such as login and registration.

I can register new users in my AspNetUsers table, but when I log in, I call: -

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

The data comes from the form correctly, and the result is the success that I expected.

Then I 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 did another project in the same way with the same setup in the past, and I had problems with null.

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'm doing wrong? This is causing serious problems now.

Hooray!

+6
source share
2 answers

Check if there is 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 the credentials you use, you should look at this

ASP.NET Identity AuthenticationManager versus SignInManager and cookie expiration

+4
source

For me it was web.config, line by line comment

 <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> 
0
source

All Articles