How to get current user, and how to use User class in MVC5?

If you’re coding in an ASP.NET MVC Controller, use using Microsoft.AspNet.Identity; … User.Identity.GetUserId(); Worth mentioning that User.Identity.IsAuthenticated and User.Identity.Name will work without adding the above mentioned using statement. But GetUserId() won’t be present without it. If you’re in a class other than a Controller, use HttpContext.Current.User.Identity.GetUserId(); In the default template of MVC 5, user ID … Read more

Unauthorised webapi call returning login page rather than 401

Brock Allen has a nice blog post on how to return 401 for ajax calls when using Cookie authentication and OWIN. http://brockallen.com/2013/10/27/using-cookie-authentication-middleware-with-web-api-and-401-response-codes/ Put this in ConfigureAuth method in the Startup.Auth.cs file: app.UseCookieAuthentication(new CookieAuthenticationOptions { AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie, LoginPath = new PathString(“/Account/Login”), Provider = new CookieAuthenticationProvider { OnApplyRedirect = ctx => { if (!IsAjaxRequest(ctx.Request)) { ctx.Response.Redirect(ctx.RedirectUri); … Read more

What is ASP.NET Identity’s IUserSecurityStampStore interface?

This is meant to represent the current snapshot of your user’s credentials. So if nothing changes, the stamp will stay the same. But if the user’s password is changed, or a login is removed (unlink your google/fb account), the stamp will change. This is needed for things like automatically signing users/rejecting old cookies when this … Read more

Do I need a Global.asax.cs file at all if I’m using an OWIN Startup.cs class and move all configuration there?

Startup.Configuration gets called slightly later than Application_Start, but I don’t think the difference will matter much in most cases. I believe the major reasons we kept the other code in Global.asax are: Consistency with previous versions of MVC. (That’s where everybody currently expects to find this code.) Ability to add other event handlers. In Global.asax, … Read more

ASP.NET Identity DbContext confusion

I would use a single Context class inheriting from IdentityDbContext. This way you can have the context be aware of any relations between your classes and the IdentityUser and Roles of the IdentityDbContext. There is very little overhead in the IdentityDbContext, it is basically a regular DbContext with two DbSets. One for the users and … Read more

ASP.NET MVC 5 – Identity. How to get current ApplicationUser

You should not need to query the database directly for the current ApplicationUser. That introduces a new dependency of having an extra context for starters, but going forward the user database tables change (3 times in the past 2 years) but the API is consistent. For example the users table is now called AspNetUsers in … Read more

HTTP Error 500.19 and error code : 0x80070021

Got precisely the same error and came to this question. As @SpaceBison mentioned in comments, this answer describes the solution – https://stackoverflow.com/a/12867753/404099. I spotted it too late and it misses some steps. This is what worked for me: Windows Server 2012, IIS 8.5. Should work for other versions too. Go to server manager, click add … Read more