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 ...
随机推荐
- 学习笔记-AngularJs(六)
在学习笔记-AngularJs(五),通过引入bootstrap.css进行改写整个样式,这时学习项目也变得好看多了,现在我们又需要目录再进行一次改变,如下图: 这样就符合之前讲的对学习目录进行布置了 ...
- laravel控制器方法中,用函数作为变量进行传递时的处理方法
本人在做上传图片时,里面执行的方法时一致的,只是个别地方不同,这种情况下,就需要把公用的部分提取出来,把不同的地方放到回调函数种去. StudentController中的方法: public fun ...
- 容错机制和熔断(Hystrix)
雪崩效应 在微服务架构中,由于服务众多,通常会涉及多个服务层级的调用,而一旦基础服务发生故障,很可能会导致级联故障,进而造成整个系统不可用,这种现象被称为服务雪崩效应.服务雪崩效应是一种因“服务提供者 ...
- JSP页面间的传值方法总结
JSP 页面间传递参数是项目中经常需要的,这应该算是 web 基本功吧.试着将各种方式总结下来,需要时可以进行权衡利弊选择最合适的方式.下面来一起看看详细的介绍: 1. URL 链接后追加参数 ? 1 ...
- Java单例模式《一》饿汉式
package com.study.mode; /** * 单例模式:饿汉式. 线程安全. * @ClassName: SingleBean * @author BlueLake * @date 20 ...
- day11- python生成式和生成器
列表生成式 列表生成式是python受欢迎的语法之一,通过一句简洁的语法就可以对一组元素进行过滤,还可以对得到的元素进行转换处理.语法格式为: [exp for val in collection i ...
- js 设置img标签的src资源无法找到的替代图片(通过img的属性设置)
在网站的前端页面设计中,要考虑到img图片资源的存在性,如果img的src图片资源不存在或显示不出来,则需要显示默认的图片.如何做到呢? 一.监听document的error事件 document.a ...
- Reveal 破解及使用
Reveal是一款很好的iOS调试应用,这在你的开发中起到了相当大的帮助. 破解包可去http://xclient.info/?_=118ba0724e7a6af91ba29a22ee4131da 下 ...
- 深入理解java虚拟机---对象的创建过程(八)
1.对象的创建过程 由于类的加载是一个很复杂的过程,所以这里暂时略过,后面会详细讲解,默认为是已加载过的类.着重强调对象的创建过程. 注意: 最后一步的init方法是代码块和构造方法. 以上是总图,下 ...
- ubuntu GUI程序开机自启设置
在 主目录下,即 $HOME路径下新建 或编辑 .gnomerc 文件,将开机启动脚本写在这里如下:gedit ~/.gnomerc#!/bin/bashcd /home/xxxx/openUI./o ...