How to add claims in ASP.NET Identity

The correct place to add claims, assuming you are using the ASP.NET MVC 5 project template is in ApplicationUser.cs. Just search for Add custom user claims here. This will lead you to the GenerateUserIdentityAsync method. This is the method that is called when the ASP.NET Identity system has retrieved an ApplicationUser object and needs 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

Where are the Login and Register pages in an AspNet Core scaffolded app?

It was announced during the preview of asp.net core 2.1 that the Identity UI would be moved to a new Razor Class Library. https://blogs.msdn.microsoft.com/webdev/2018/03/02/aspnetcore-2-1-identity-ui/ It is still possible to scaffold the Identity Views into your own project if you prefer local views: https://docs.microsoft.com/en-us/aspnet/core/security/authentication/scaffold-identity?view=aspnetcore-2.1&tabs=visual-studio

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