mvc core2.1 Identity.EntityFramework Core 配置 (一)
https://docs.microsoft.com/zh-cn/aspnet/core/security/authentication/customize_identity_model?view=aspnetcore-2.1 参考地址
标识模型包含七个实体类型:
User-表示的用户Role-表示的角色UserClaim-表示用户拥有的声明UserToken-表示用户的身份验证令牌UserLogin-将用户与一个登录名相关联RoleClaim-表示将授予角色中的所有用户的声明UserRole-加入将用户和角色相关联的实体
实体类型关系
这些实体类型通过以下方式彼此相关:
- 每个
User可以具有许多UserClaims - 每个
User可以具有许多UserLogins - 每个
User可以具有许多UserTokens - 每个
Role可以具有许多关联RoleClaims - 每个
User可以具有许多关联Roles,和每个Role可以与多个用户相关联- 这是一个多对多关系,这需要在数据库中的联接表。 联接表均由表示
UserRole实体。
- 这是一个多对多关系,这需要在数据库中的联接表。 联接表均由表示
dotnet new mvc -o IdentityMvc
cd IdentityMvc
dotnet add package Microsoft.AspNetCore.Identity.EntityFrameworkCore
using IdentityMvc.Data;
using Microsoft.EntityFrameworkCore;
using IdentityMvc.Models;
using Microsoft.AspNetCore.Identity;
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders(); services.Configure<IdentityOptions>(options =>
{
// Password settings
options.Password.RequireDigit = true;
options.Password.RequiredLength = ;
options.Password.RequireNonAlphanumeric = false;
options.Password.RequireUppercase = true;
options.Password.RequireLowercase = false;
options.Password.RequiredUniqueChars = ; // Lockout settings
options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes();
options.Lockout.MaxFailedAccessAttempts = ;
options.Lockout.AllowedForNewUsers = true; // User settings
options.User.RequireUniqueEmail = true;
}); services.ConfigureApplicationCookie(options =>
{
// Cookie settings
options.Cookie.HttpOnly = true;
options.Cookie.Expiration = TimeSpan.FromDays();
// If the LoginPath isn't set, ASP.NET Core defaults
// the path to /Account/Login.
options.LoginPath = "/Account/Login";
// If the AccessDeniedPath isn't set, ASP.NET Core defaults
// the path to /Account/AccessDenied.
options.AccessDeniedPath = "/Account/AccessDenied";
options.SlidingExpiration = true;
});
Data->ApplicationDbContext 新建
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using IdentityMvc.Models; namespace IdentityMvc.Data
{
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
} protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
// Customize the ASP.NET Identity model and override the defaults if needed.
// For example, you can rename the ASP.NET Identity table names and more.
// Add your customizations after calling base.OnModelCreating(builder);
}
}
}
Models->ApplicationUser 新建
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity; namespace IdentityMvc.Models
{
// Add profile data for application users by adding properties to the ApplicationUser class
public class ApplicationUser : IdentityUser
{
}
}
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 Identity.EntityFramework Core 配置 (一)的更多相关文章
- mvc core2.1 Identity.EntityFramework Core 实例配置 (四)
https://docs.microsoft.com/zh-cn/aspnet/core/security/authentication/customize_identity_model?view=a ...
- mvc core2.1 Identity.EntityFramework Core ROle和用户绑定查看 (八)完成
添加角色属性查看 Views ->Shared->_Layout.cshtml <div class="navbar-collapse collapse"> ...
- mvc core2.1 Identity.EntityFramework Core 用户Claims查看(七)
添加角色属性查看 Views ->Shared->_Layout.cshtml <div class="navbar-collapse collapse"> ...
- mvc core2.1 Identity.EntityFramework Core 注册 (二)
Startup.cs-> Configure app.UseAuthentication(); //启动验证 Controllers->AccountController.cs 新建 us ...
- mvc core2.1 Identity.EntityFramework Core 导航状态栏(六)
之前做的无法 登录退出,和状态,加入主页导航栏 Views ->Shared->_Layout.cshtml <div class="navbar-collapse col ...
- mvc core2.1 Identity.EntityFramework Core 用户列表预览 删除 修改 (五)
用户列表预览 Controllers->AccountController.cs [HttpGet] public IActionResult Index() { return View(_us ...
- mvc core2.1 Identity.EntityFramework Core 登录 (三)
Controllers->AccountController.cs 新建 [HttpGet] [AllowAnonymous] public async Task<IActionResul ...
- webapi core2.1 Identity.EntityFramework Core进行配置和操作数据 (一)没什么用
https://docs.microsoft.com/en-us/aspnet/core/security/authentication/identity?view=aspnetcore-2.1&am ...
- webapi core2.1 IdentityServer4.EntityFramework Core进行配置和操作数据
https://identityserver4.readthedocs.io/en/release/quickstarts/8_entity_framework.html 此连接的实践 vscode ...
随机推荐
- linux网络连接--桥接bridge,NAT,host-only的区别
linux网络连接主要分为三种:桥接,net,host_only 桥接使用的是真实网卡,电脑里面有两种真实网卡,有线网卡,无线网卡,当你使用的是无线连接, 则选择无线网卡,使用网线连接,则选择有线网卡 ...
- ubuntu中更新.netcore到2.1版本
如果需要安装新版本到dotnetcore,需要先卸载旧版本(https://github.com/dotnet/core/blob/master/release-notes/download-arch ...
- VS2010安装项目程序打包操作详解
(转自:http://blog.sina.com.cn/s/blog_74f702e60101at62.html) 1.打开VS2010,选择 新建项目---其他项目类型---Visual Studi ...
- js将接口返回的数据序列化
<div style={{marginLeft: '80px'}}> <pre> {th ...
- day16-python常用的内置模块2
logging模块的使用 一:日志是我们排查问题的关键利器,写好日志记录,当我们发生问题时,可以快速定位代码范围进行修改.Python有给我们开发者们提供好的日志模块,下面我们就来介绍一下loggin ...
- 在docker hub,用github的dockerfile自动生成docker镜像
简介: 我已经深深的爱上了docker技术. 在日常使用中,经常看到docker hub 中有很多autobuild的镜像.基本使用是在github中上传dockerfile,过一会儿,docker ...
- 每天CSS学习之direction
direction是CSS2的属性,它的作用是规定文字书写的方向. 1.ltr:从左到右,即left to right.该值为默认值.如下所示: div{ border:1px solid red; ...
- Map中根据条件删除元素
今天在写程序过程中,需要根据判断条件删除一个Map中的相应数据,我自然而然想到可以通过调用Map中的remove(Object key)函数进行删除:代码如下: public Map<Doubl ...
- memset详解 设置无穷大INF
memest原型 (please type "man memset" in your shell) void *memset(void *s, int c, size_t n); ...
- sass 变量的声明 嵌套
sass 的默认变量一般是用来设置默认值,然后根据需求来覆盖的,覆盖的方式也很简单,只需要在默认变量之前重新声明下变量即可. $baseLineHeight: 2; $baseLineHeight: ...