44- EF + Identity实现
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实现的更多相关文章
- 【ASP.NET Core快速入门】(十四)MVC开发:UI、 EF + Identity实现、注册实现、登陆实现
前言 之前我们进行了MVC的web页面的Cookie-based认证实现,接下来的开发我们要基于之前的MvcCookieAuthSample项目做修改. MvcCookieAuthSample项目地址 ...
- 菜鸟入门【ASP.NET Core】14:MVC开发:UI、 EF + Identity实现、注册实现、登陆实现
前言 之前我们进行了MVC的web页面的Cookie-based认证实现,接下来的开发我们要基于之前的MvcCookieAuthSample项目做修改. MvcCookieAuthSample项目地址 ...
- 任务44:Identity MVC: EF + Identity实现
使用VSCode开发 Razer的智能感知不好.所以这里切换为VS2017进行开发: 新建一个Data的文件夹来存放我们的DBContext.在Data文件夹下新建: ApplicationDbCon ...
- 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 ...
- ASP.NET Core快速入门_学习笔记汇总
第2章 配置管理 任务12:Bind读取配置到C#实例 任务13:在Core Mvc中使用Options 任务14:配置的热更新 任务15:配置框架设计浅析 第3章 依赖注入 任务16:介绍- 任务1 ...
- ASP.NET Core快速入门(第6章:ASP.NET Core MVC)--学习笔记
课程链接:http://video.jessetalk.cn/course/explore 良心课程,大家一起来学习哈! 任务40:介绍 1.Individual authentication 模板 ...
- 【笔记目录1】【jessetalk 】ASP.NET Core快速入门_学习笔记汇总
当前标签: ASP.NET Core快速入门 共2页: 1 2 下一页 任务50:Identity MVC:DbContextSeed初始化 GASA 2019-03-02 14:09 阅读:16 ...
- NET5 Web应用程序
ASP.NET5 Web应用程序结构 本文参考ASP.NET5 官方文档 Understanding ASP.NET 5 Web Apps,加入了一些个人理解,理解不对的地方希望大家能指出,互相学习. ...
- 了解ASP.NET5 Web应用程序结构
本文参考ASP.NET5 官方文档 Understanding ASP.NET 5 Web Apps,加入了一些个人理解,理解不对的地方希望大家能指出,互相学习. ASP.NET 5 针对WEB编程引 ...
- asp.net core 系列 2 启动Startup类介绍
一.Startup类 ASP.NET Core 应用是一个控制台应用,它在其 Program.Main 方法中创建 Web 服务器.其中Main方法是应用的托管入口点,Main 方法调用 WebHos ...
随机推荐
- VC++动态链接库(DLL)编程
一.概论 1:http://www.pconline.com.cn/pcedu/empolder/gj/vc/0509/698632.html 2:http://pcedu.pconline.com. ...
- 【CentOS】在Centos7 下无图形界面安装 Oracle11g
目标 - 在虚拟机CentOS7中无图形界面安装Oracle11G R2版本 ① 系统要求以及准备 1. 物理内存不小于1G: 查看方式: # grep MemTotal /proc/meminfo ...
- npm 安装axios和使用增删改查
1:安装axios(建议安装淘宝镜像) npm install axios 2:项目导入 npm install --save axios vue-axios 3:页面导入 import axios ...
- JS interview loop code
//九九乘法表 document.write("<table width='600' border=0'>"); for(var i=1; i<=9; i++){ ...
- June 06th 2017 Week 23rd Tuesday
At the touch of love, everyone becomes a poet. 一谈到爱,每个人都变成了一位诗人. Sweet words always have the power o ...
- 使用SAPGUI画图
国内80后上的编程课应该都学过Logo这门编程语言: Logo语言是一门专门设计用来进行编程教学的语言,于1967年由Wally Feurzeig, Seymour Papert和Cynthia So ...
- IOS CoreLocation框架的使用(用于地理定位)
● 在移动互联网时代,移动app能解决用户的很多生活琐事,比如 ● 导航:去任意陌生的地方 ● 周边:找餐馆.找酒店.找银行.找电影院 ● 在上述应用中,都用到了地图和定位功能,在iOS开发中 ...
- Struts2学习笔记——Struts2搭建和第一个小程序
1.新建web项目 2.配置Struts2核心过滤器 (1)打开web.xml文件,做以下配置: <?xml version="1.0" encoding="UTF ...
- 运行出现Server Tomcat v8.5 Server at localhost failed to start.和A child container failed during start
出现问题: 解决方法: 1.看servlet文件中的@WebServlet“()”,里面是否少了/字符,如图: 加上即可,有问题随时留言,欢迎您的咨询!
- mysql时间日期函数
now(), current_timestamp(); -- 当前日期时间 current_date(); -- 当前日期 current_time(); -- 当前时间 date('yyyy-mm- ...