.net Core Identity and MySql on Mac

From Logic Wiki
Jump to: navigation, search


csproj file

 <ItemGroup>
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.5" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="5.0.5">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
    <PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="5.0.0-alpha.2" />
    <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="3.1.0" />
    <PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="3.1.0" />
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.0.0" />
    <PackageReference Include="Microsoft.AspNetCore.Identity" Version="2.2.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.5" />
  </ItemGroup>

Notes

Even we use MySQL in our project we have to install SqlServer to build identity related files Pomelo's version Identity.UI's version and CodeGeneration.Design's version numbers are important.


startup.cs

public void ConfigureServices(IServiceCollection services)
{
  string mySqlConnectionStr = Configuration.GetConnectionString("DefaultConnection");
  services.AddDbContext<AppDbContext>(options => options.UseMySql(mySqlConnectionStr, ServerVersion.AutoDetect(mySqlConnectionStr), b => b.MigrationsAssembly("Tarotor")));
  services.AddIdentity<User, Role>(options =>
  {
    options.Password.RequiredLength = 5;
    options.Password.RequireNonAlphanumeric = true;
    options.Password.RequireUppercase = true;
    options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(1d);
    options.Lockout.MaxFailedAccessAttempts = 5;
   })
     .AddEntityFrameworkStores<AppDbContext>()
     .AddDefaultTokenProviders();
  services.AddControllersWithViews();
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
...
  app.UseRouting();
  app.UseAuthorization();
...
}

appsettings.json

 "ConnectionStrings": {
    "DefaultConnection": "server=localhost; port=3306; database=Tarotor; user=orderlogicuser; password=0rderl0gicuser; Persist Security Info=False; Connect Timeout=300",
    "AppDbContextConnection": "Server=(localdb)\\mssqllocaldb;Database=Tarotor;Trusted_Connection=True;MultipleActiveResultSets=true"
  }

DbContext

 public class AppDbContext : IdentityDbContext<User, Role, Guid>
    {
        public AppDbContext(DbContextOptions<AppDbContext> options)
            : base(options)
        {
 
        }
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            base.OnConfiguring(optionsBuilder);
        }
    }

User and Role Classes

User.cs

    public class User : IdentityUser<Guid>
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }

Role.cs

    public class Role : IdentityRole<Guid>
    {

    }

Command Line

dotnet tool install -g dotnet-aspnet-codegenerator 
dotnet restore

Now we can create UI

dotnet aspnet-codegenerator identity --listFiles
dotnet aspnet-codegenerator identity --files="Account.Manage.ChangePassword;Account.Register;Account.ResetPassword;Account.ResetPasswordConfirmation"

or simply rund defaults by

dotnet aspnet-codegenerator identity --useDefaultUI