1-配置EF, 需要创建如下几个类

默认User主键为guid类型,现在改成int类型

namespace MvcCookieAuthSample.Models
{
public class ApplicationUser:IdentityUser<int>
{ }
namespace MvcCookieAuthSample.Models
{
public class ApplicationUserRole:IdentityRole<int>
{ }
}
namespace MvcCookieAuthSample.Data
{
public class ApplicationDbContext:IdentityDbContext<ApplicationUser,ApplicationUserRole,int>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options):base(options)
{ }
}
}

在Startup.cs 启用EF

    public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options => {
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
});
}

在appsetting.json配置连接数据字符串

"ConnectionStrings": {
"DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=aspnet-IdentitySample-C9275B98-9E63-4E35-AE50-5F96CA59C41D;Trusted_Connection=True;MultipleActiveResultSets=true"
}

使用Nuget增加   Microsoft.EntityFrameworkCore.Tools

然后在控制台运行相关EF命令生成数据库表,  http://www.siyouku.cn/article/6871.html

E:\coding\netcore\MvcCookieAuthSample>dotnet ef migrations add VSInit
E:\coding\netcore\MvcCookieAuthSample>dotnet ef database update

  

2-启用验证

        public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options=> {
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
}); services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(options=>{//自定义登陆地址,不配置的话则默认为http://localhost:5000/Account/Login
options.LoginPath="/Account/Login";
}); services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
}); services.AddIdentity<ApplicationUser, ApplicationUserRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders(); services.Configure<IdentityOptions>(option=> {
option.Password.RequireLowercase = false;
option.Password.RequireNonAlphanumeric = false;
option.Password.RequireUppercase = false;
}); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}

注册账号

  public class AccountController : Controller
{
private UserManager<Models.ApplicationUser> _userManager;
private SignInManager<Models.ApplicationUser> _signInManager;
public AccountController(UserManager<Models.ApplicationUser> userManager,
SignInManager<Models.ApplicationUser> signInManager)
{
_signInManager = signInManager;
_userManager = userManager;
} [HttpPost]
public async Task<IActionResult> Register(ViewModel.RegisterViewModel registerViewModel)
{
Models.ApplicationUser applicationUser = new Models.ApplicationUser()
{
Email = registerViewModel.Email,
UserName = registerViewModel.Email,
NormalizedEmail = registerViewModel.Email
}; IdentityResult result = await _userManager.CreateAsync(applicationUser, registerViewModel.Password);
if (result.Succeeded)
{
return RedirectToAction("Index", "Home");
}
return View();
} }

44- EF + Identity实现的更多相关文章

  1. 【ASP.NET Core快速入门】(十四)MVC开发:UI、 EF + Identity实现、注册实现、登陆实现

    前言 之前我们进行了MVC的web页面的Cookie-based认证实现,接下来的开发我们要基于之前的MvcCookieAuthSample项目做修改. MvcCookieAuthSample项目地址 ...

  2. 菜鸟入门【ASP.NET Core】14:MVC开发:UI、 EF + Identity实现、注册实现、登陆实现

    前言 之前我们进行了MVC的web页面的Cookie-based认证实现,接下来的开发我们要基于之前的MvcCookieAuthSample项目做修改. MvcCookieAuthSample项目地址 ...

  3. 任务44:Identity MVC: EF + Identity实现

    使用VSCode开发 Razer的智能感知不好.所以这里切换为VS2017进行开发: 新建一个Data的文件夹来存放我们的DBContext.在Data文件夹下新建: ApplicationDbCon ...

  4. NET CORE Learning

    ASP.NET Core 基础教程https://www.cnblogs.com/lonelyxmas/tag/ASP.NET%20Core%20%E5%9F%BA%E7%A1%80%E6%95%99 ...

  5. ASP.NET Core快速入门_学习笔记汇总

    第2章 配置管理 任务12:Bind读取配置到C#实例 任务13:在Core Mvc中使用Options 任务14:配置的热更新 任务15:配置框架设计浅析 第3章 依赖注入 任务16:介绍- 任务1 ...

  6. ASP.NET Core快速入门(第6章:ASP.NET Core MVC)--学习笔记

    课程链接:http://video.jessetalk.cn/course/explore 良心课程,大家一起来学习哈! 任务40:介绍 1.Individual authentication 模板 ...

  7. 【笔记目录1】【jessetalk 】ASP.NET Core快速入门_学习笔记汇总

    当前标签: ASP.NET Core快速入门 共2页: 1 2 下一页  任务50:Identity MVC:DbContextSeed初始化 GASA 2019-03-02 14:09 阅读:16 ...

  8. NET5 Web应用程序

    ASP.NET5 Web应用程序结构 本文参考ASP.NET5 官方文档 Understanding ASP.NET 5 Web Apps,加入了一些个人理解,理解不对的地方希望大家能指出,互相学习. ...

  9. 了解ASP.NET5 Web应用程序结构

    本文参考ASP.NET5 官方文档 Understanding ASP.NET 5 Web Apps,加入了一些个人理解,理解不对的地方希望大家能指出,互相学习. ASP.NET 5 针对WEB编程引 ...

  10. asp.net core 系列 2 启动Startup类介绍

    一.Startup类 ASP.NET Core 应用是一个控制台应用,它在其 Program.Main 方法中创建 Web 服务器.其中Main方法是应用的托管入口点,Main 方法调用 WebHos ...

随机推荐

  1. linux内核编译与开发

    一.Linux内核简介linux kernel map: linux 系统体系结构: linux kernel体系结构: arm有7种工作模式,x86也实现了4个不同级别RING0-RING3,RIN ...

  2. OC property(声明)

    @interface Student : NSObject { int _age; int _no; float _height; } // 当编译器遇到@property时,会自动展开成getter ...

  3. Matlab Colour Theme

    [转]http://blog.csdn.net/df865017/article/details/48164429 使用MATLAB进行编码时, 长时间面对白底黑字的屏幕, 眼睛会疼! 因此, 选择一 ...

  4. JavaScript数组实战小练习

    1.找出元素在数组中的位置. function indexOf(arr, item) { if(Array.prototype.indexOf){ //判断浏览器是否支持indexOf方法 retur ...

  5. java循环作业0912

    题目一:一张纸的厚度大约是0.08mm,对折多少次之后能达到珠穆朗玛峰的高度(8848.13米)? double a = 0.08; double h =0; int i=0; for(i=1;h&l ...

  6. 初入AngularJS基础门

    作为mvvm 框架过重 不适用于性能比较高的移动端的web栈, ui组建性对复杂,不利于重用 AngularJS 构建一个CRUD ( create retrieve update delete )的 ...

  7. 简单实用的.htaccess文件配置

    .htaccess 文件 (Hypertext Access file) 是Apache Web服务器的一个非常强大的配置文件,对于这个文件,Apache有一堆参数可以让你配置出几乎随心所欲的功能.. ...

  8. how to create a custom form for sharepoint list

    在VS中创建一个applicationPage映射到Layouts文件夹下,然后代码如下: SPList lstTest = web.Lists["Shared Documents" ...

  9. Vue node.js商城-购物车模块

      一.渲染购物车列表页面 新建src/views/Cart.vue获取cartList购物车列表数据就可以在页面中渲染出该用户的购物车列表数据 data(){   return {      car ...

  10. 为什么需要Vlan ? Vlan实现原理 ? 不同Vlan的通信 ?

    好文章!!良心推荐!!! 原文链接 https://blog.csdn.net/cwm_meng_home/article/details/49762807