码迷,mamicode.com
首页 > Web开发 > 详细

mvc core2.1 IdentityServer.EntityFramework Core 配置

时间:2018-08-03 10:45:26      阅读:292      评论:0      收藏:0      [点我收藏+]

标签:linq   nbsp   creating   failed   nal   .net   EAP   defaults   isp   

dotnet new mvc -o  IdentityMvc

cd IdentityMvc

dotnet add package Microsoft.AspNetCore.Identity.EntityFrameworkCore
 
Startup.cs->ConfigureServices

using IdentityMvc.Data;
using Microsoft.EntityFrameworkCore;
using IdentityMvc.Models;
using Microsoft.AspNetCore.Identity;

技术分享图片
 1  services.AddDbContext<ApplicationDbContext>(options =>
 2                 options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
 3 
 4             services.AddIdentity<ApplicationUser, IdentityRole>()
 5                 .AddEntityFrameworkStores<ApplicationDbContext>()
 6                 .AddDefaultTokenProviders();
 7 
 8             services.Configure<IdentityOptions>(options =>
 9             {
10                 // Password settings
11                 options.Password.RequireDigit = true;
12                 options.Password.RequiredLength = 8;
13                 options.Password.RequireNonAlphanumeric = false;
14                 options.Password.RequireUppercase = true;
15                 options.Password.RequireLowercase = false;
16                 options.Password.RequiredUniqueChars = 6;
17 
18                 // Lockout settings
19                 options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(30);
20                 options.Lockout.MaxFailedAccessAttempts = 10;
21                 options.Lockout.AllowedForNewUsers = true;
22 
23                 // User settings
24                 options.User.RequireUniqueEmail = true;
25             });
26 
27             services.ConfigureApplicationCookie(options =>
28             {
29                 // Cookie settings
30                 options.Cookie.HttpOnly = true;
31                 options.Cookie.Expiration = TimeSpan.FromDays(150);
32                 // If the LoginPath isn‘t set, ASP.NET Core defaults 
33                 // the path to /Account/Login.
34                 options.LoginPath = "/Account/Login";
35                 // If the AccessDeniedPath isn‘t set, ASP.NET Core defaults 
36                 // the path to /Account/AccessDenied.
37                 options.AccessDeniedPath = "/Account/AccessDenied";
38                 options.SlidingExpiration = true;
39             });
View Code

Data->ApplicationDbContext 新建

技术分享图片
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Threading.Tasks;
 5 using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
 6 using Microsoft.EntityFrameworkCore;
 7 using IdentityMvc.Models;
 8 
 9 namespace IdentityMvc.Data
10 {
11     public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
12     {
13         public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
14             : base(options)
15         {
16         }
17 
18         protected override void OnModelCreating(ModelBuilder builder)
19         {
20             base.OnModelCreating(builder);
21             // Customize the ASP.NET Identity model and override the defaults if needed.
22             // For example, you can rename the ASP.NET Identity table names and more.
23             // Add your customizations after calling base.OnModelCreating(builder);
24         }
25     }
26 }
View Code

Models->ApplicationUser 新建

技术分享图片
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Threading.Tasks;
 5 using Microsoft.AspNetCore.Identity;
 6 
 7 namespace IdentityMvc.Models
 8 {
 9     // Add profile data for application users by adding properties to the ApplicationUser class
10     public class ApplicationUser : IdentityUser
11     {
12     }
13 }
View Code

appsettings.json 加入数据库连接

  "ConnectionStrings": {
    "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=aspnet-ids-3D54E4B2-38C1-466C-A12F-E9CCF493B11B;Trusted_Connection=True;MultipleActiveResultSets=true"
  },

最后生成编译,

生成数据库映射表

更新数据库

dotnet build
dotnet ef migrations add Initial -o Data/Migrations
dotnet ef database update

 

mvc core2.1 IdentityServer.EntityFramework Core 配置

标签:linq   nbsp   creating   failed   nal   .net   EAP   defaults   isp   

原文地址:https://www.cnblogs.com/LiuFengH/p/9411335.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!