Asp.net Identity password hashing

HEALTH WARNING for the below answer: Know which version of ASP.Net Identity you are using. You should refer to the source code directly if it is one of the newer versions from the github repository. As I write this, the current version (3.0.0-rc1/…/PasswordHasher.cs) of the password handler is significantly different to the below answer. This … Read more

IdentityServer4 Role Based Authorization for Web API with ASP.NET Core Identity

The problem is that the claims are not added to the access token. There are two tokens, the access token and the identity token. When you want to add claims to the identity token, then you’ll have to configure the IdentityResource. If you want to add claims to the access token, then you’ll have to … Read more

Updating user data – ASP.NET Identity

OK… I spent hours trying to figure why userManager.updateAsync would not persist the user data that we edit … until I reached the following conclusion: The confusion arises from the fact that we create the UserManager in one line like this: var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new MyDbContext())); …then we use manager.UpdateAsync( user ); but … Read more

How to localize ASP.NET Identity UserName and Password error messages?

For ASP.NET Core: (Microsoft.AspNetCore.Identity 1.0.0) Create a class that inherits IdentityErrorDescriber and override the desired error messages. public class CustomIdentityErrorDescriber : IdentityErrorDescriber { public override IdentityError DefaultError() { return new IdentityError { Code = nameof(DefaultError), Description = $”An unknown failure has occurred.” }; } public override IdentityError ConcurrencyFailure() { return new IdentityError { Code = … Read more

How to use JWT in MVC application for authentication and authorization?

In order for MVC to understand anything about your JWT you basically have to tell it 🙂 . First, install the Jwt package from nuget: Install-Package Microsoft.Owin.Security.Jwt Then open up your Startup.cs file and add a new funtion that will tell MVC how to consume JWT. At basics your Startup will look something like: using … Read more

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

Adding ASP.NET MVC5 Identity Authentication to an existing project

Configuring Identity to your existing project is not hard thing. You must install some NuGet package and do some small configuration. First install these NuGet packages with Package Manager Console: PM> Install-Package Microsoft.AspNet.Identity.Owin PM> Install-Package Microsoft.AspNet.Identity.EntityFramework PM> Install-Package Microsoft.Owin.Host.SystemWeb Add a user class and with IdentityUser inheritance: public class AppUser : IdentityUser { //add your … 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