How does a new ASP.NET MVC 5 application know how to create a database and how does the Account Controller access the database?

1) WHAT’S GOING ON HERE: When you create a new MVC 5 application and choose “Individual User Accounts“, a new ASP.NET Identity Provider is included which uses Entity Framework 6 Code-First. Microsoft has adopted EF-Code-First to make Identity as customizable as possible. When Identity is accessed for the first time, Entity Framework checks to see … Read more

Why is Asp.Net Identity IdentityDbContext a Black-Box?

The ApplicationDbContext‘s Users and Roles properties are mapped to the AspNetUsers and AspNetRoles tables, and the rest of the entities (Claims, Logins, UserRoles) are mapped automatically via navigation properties. As far as I know, the prefixing of table names with “AspNet” are the only custom mappings in ApplicationDbContext, everything else is just Entity Framework Code … Read more

What is the Signing Credential in IdentityServer4?

The Authorization Server will sign tokens with a key. Resource Server(s) should verify that the token’s integrity with a key. Together they form a (usually asymmetric, e.g. public/private) key (pair). By default IdentityServer will publish the public key for verifying tokens via the /.well-known/openid-configuration endpoint. For development scenarios, you typically want to skip the fuss … Read more

Prevent login when EmailConfirmed is false

You need to add a few lines to the Login action (POST method) to verify that the user has confirmed their email. The method you want to check is UserManager.IsEmailConfirmed. Here is what your Login action will look like. public async Task<ActionResult> Login(LoginViewModel model, string returnUrl) { if (ModelState.IsValid) { var user = await UserManager.FindByNameAsync(model.Email); … Read more

Identity Provider and Unity Dependency Injection

Ok, I have resolved my problem, I have injected the dependencies with this method, but I don’t understand very well why it’s work… Now, Identity works fine with my Unity DI container.RegisterType<AccountController>(new InjectionConstructor()); container.RegisterType<RolesAdminController>(new InjectionConstructor()); container.RegisterType<ManageController>(new InjectionConstructor()); container.RegisterType<UsersAdminController>(new InjectionConstructor());

How to make EF-Core use a Guid instead of String for its ID/Primary key

You need custom ApplicationUser inherit from IdentityUser<TKey> and custom Role inherit from IdentityRole<TKey> public class ApplicationUser : IdentityUser<Guid> { } public class Role : IdentityRole<Guid> { } Custom context class inherit from IdentityDbContext<ApplicationUser, Role, TKey> and use fluent api for auto generate guid keys. public class ApplicationDbContext : IdentityDbContext<ApplicationUser, Role, Guid> { protected override void … Read more

How to protect all controllers by default with bearer token in ASP.NET Core?

Starting with .Net 6 we can do this (if using minimal hosting model recommended by Microsoft): app .MapControllers() .RequireAuthorization(); // This will set a default policy that says a user has to be authenticated Starting with .Net Core 3 we can do this: app.UseEndpoints(endpoints => { endpoints .MapControllers() .RequireAuthorization(); // This will set a default … Read more

How to add ASP.NET MVC5 Identity Authentication to existing database

1) If I want to use identity authentication, is there a reason not to use MVC template? Or is there a reason to use empty template? MVC template also provides bootstrap. Identity authentication, MVC template and bootstrap are not really related. If you create new MVC 5 application, AccountController is created for you. You can … Read more

ASP.Net Core Identity login status lost after deploy

(solution split into a separate answer following Chris comment) I found a solution to keep the login status, it survives website stop/start, and an update of the website source folder: public void ConfigureServices(IServiceCollection services) { services.AddDataProtection() // This helps surviving a restart: a same app will find back its keys. Just ensure to create the … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)