Redirect to login when unauthorized in ASP.NET Core

With the current aspnet core version (2.1.0), this has changed, now you can use the extensions: services.ConfigureApplicationCookie(options => options.LoginPath = “/login”); or services .AddAuthentication() .AddCookie(options => { options.LoginPath = “/login”; options.LogoutPath = “/logout”; }); You can see more about migrating in to 2.0 in this article.

Asp.net Identity Validation Error

Your code does’t show this, but from the errors you are getting I assume that you are overriding OnModelCreating.This is where IdentityDbContext<ApplicationUser> configure the entity framework mappings. This means that if you want to override OnModelCreating you need to either call the base or you must do the mapping yourself. So either this: protected override … Read more

Can not instantiate proxy…Could not find a parameterless constructor

MOQ is good for mocking interfaces, but does not work so well with concrete classes. So instead of mocking concrete class, ask for the inteface: var userStore = new Mock<IUserStore<PreRegistrationMVC.Models.ApplicationUser>>(); Also ApplicationUser should be POCO, so no need to mock it, just create its instance without MOQ and use in tests.

How To Change Password Validation in ASP.Net MVC Identity 2?

In the MVC project template in VS2013 Update 2, there should be a file called App_Start/IdentityConfig.cs. In it you should find the class ApplicationUserManager and a static factory method called Create(). That’s where the user manager class is configured, including the server-side validation rules for passwords are defined. For example: manager.PasswordValidator = new PasswordValidator { … Read more

How to Seed Users and Roles with Code First Migration using Identity ASP.NET Core

My way of doing this is to create a class in models namespace. public class SampleData { public static void Initialize(IServiceProvider serviceProvider) { var context = serviceProvider.GetService<ApplicationDbContext>(); string[] roles = new string[] { “Owner”, “Administrator”, “Manager”, “Editor”, “Buyer”, “Business”, “Seller”, “Subscriber” }; foreach (string role in roles) { var roleStore = new RoleStore<IdentityRole>(context); if (!context.Roles.Any(r … Read more

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