把旧系统迁移到.Net Core 2.0 日记(11) -- Authentication 认证 claimsIdentity 对比 之前的FormAuthentication
实现最简单的认证,类似之前的FormAuthentication
在 Startup 的 ConfigureServices() 方法中添加 Authentication 的配置: 这个CookieAuthenticationDefaults类默认的登录地址是/Account/Login,如果要要修改
则可以在后面的AddCookie()方法里修改路径
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
}).AddCookie();
在 Startup 的 Configure() 方法
app.UseAuthentication();
AccountController方法
public class AccountController : Controller
{
private readonly CRMContext _context;
public AccountController(CRMContext context)
{
_context = context;
}
[AllowAnonymous]
[HttpGet]
public IActionResult Login()
{
return View();
}
public async Task<IActionResult> Logout()
{
await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
return RedirectToAction("Login");
}
[AllowAnonymous]
[HttpPost]
public async Task<IActionResult> Login(IFormCollection form)
{
string userName = form["txtLoginId"];
string pwd = form["txtPwd"];
if ( == new UserLogic(_context).UserLogin(userName, pwd))
{
var claimsIdentity = new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.Name, userName) }, "Basic");
var claimsPrincipal = new ClaimsPrincipal(claimsIdentity);
await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, claimsPrincipal);
return Json(new { isSuccess = true, message = "登录成功" });
}
else
{
return Json(new { isSuccess = false, message = "登录失败" });
} }
我们之前只能把登录的用户名放在FormAuthentication的cookie里, 使用时就用User.Identity.Name获得当前登录的用户名,
但是现在我们可以把其他的信息,如UserId,SystemId都放到ClaimsIdentity里. 这样写
var claimsIdentity = new ClaimsIdentity(new Claim[]
{
new Claim(ClaimTypes.Name, userName),
new Claim(ClaimTypes.Sid, ""),
new Claim(ClaimTypes.System,"HR")
}, "Basic"); //使用方法
//User.Claims.FirstOrDefault(t => t.Type == System.Security.Claims.ClaimTypes.Sid).Value
//User.Claims.FirstOrDefault(t => t.Type == System.Security.Claims.ClaimTypes.System).Value
登录提交Form的参数, 要改成IFormCollection,否则会出错
The 'Microsoft.AspNetCore.Mvc.ModelBinding.Binders.FormCollectionModelBinder' cannot bind to a model of type 'Microsoft.AspNetCore.Http.FormCollection'.
Change the model type to 'Microsoft.AspNetCore.Http.IFormCollection' instead.
System.Security.Cryptography.HashAlgorithm.Create(string hashName) Hash方法还没实现,会出现错误
解决方法, 要添加nuget包
https://stackoverflow.com/questions/35363358/computing-sha1-with-asp-net-core
这样写
var sha1 = System.Security.Cryptography.SHA1.Create();
参考文章:
https://www.cnblogs.com/seriawei/p/7452743.html
http://www.cnblogs.com/dudu/p/7631927.html
http://www.cnblogs.com/dudu/p/6368240.html
http://www.cnblogs.com/bidianqing/p/6870163.html
http://www.cnblogs.com/tdfblog/p/aspnet-core-security-authentication-cookie.html
http://www.cnblogs.com/RainingNight/p/introduce-basic-authentication-in-asp-net-core.html
把旧系统迁移到.Net Core 2.0 日记(11) -- Authentication 认证 claimsIdentity 对比 之前的FormAuthentication的更多相关文章
- 把旧系统迁移到.Net Core 2.0 日记 (18) --JWT 认证(Json Web Token)
我们最常用的认证系统是Cookie认证,通常用一般需要人工登录的系统,用户访问授权范围的url时,会自动Redirect到Account/Login,登录后把认证结果存在cookie里. 系统只要找到 ...
- 把旧系统迁移到.Net Core 2.0 日记 (15) --Session 改用Redis
安装Microsoft.Extensions.Caching.Redis.Core NuGet中搜索Microsoft.Extensions.Caching.Redis.Core并安装,此NuGet包 ...
- 把旧系统迁移到.Net Core 2.0 日记(1) - Startup.cs 解析
因为自己到开发电脑转到Mac Air,之前的Webform/MVC应用在Mac 跑不起来,而且.Net Core 2.0 已经比较稳定了. 1. 为什么会有跨平台的.Net Core 近年来,我们已 ...
- 把旧系统迁移到.Net Core 2.0 日记 (12) --发布遇到的问题
1. 开发时是在Mac+MySql, 尝试发布时是在SQL2005+Win 2008 (第一版) 在Startup.cs里,数据库连接要改,分页时netcore默认是用offset关键字分页, 如果用 ...
- 把旧系统迁移到.Net Core 2.0 日记 (17) --多租户和SoftDelete
在EF Core 2.0版本中出现了全局过滤新特性即HasQueryFilter,它出现的意义在哪里?能够解决什么问题呢? 通过HasQueryFilter方法来创建过滤器能够允许我们对访问特定数据库 ...
- 把旧系统迁移到.Net Core 2.0 日记(10) -- EF core 和之前版本多对多映射区别
EF Core 现在不支持多对多映射,只能做2个一对多映射. 比如Product和Category 我现在定义Product和Category是多对多关系. 那么实体定义如下: public clas ...
- 把旧系统迁移到.Net Core 2.0 日记(8) - EASYUI datagrid+ Dapper+ 导出Excel
迁移也没太大变化,有一个, 之前的Request.QueryString 是返回NameValueCollection, 现在则是返回整个字符串. 你要改成Request.Query[“key”] 直 ...
- 把旧系统迁移到.Net Core 2.0 日记(5) Razor/HtmlHelper/资源文件
net core 的layout.cshtml文件有变化, 区分开发环境和非开发环境. 开发环境用的是非压缩的js和css, 正式环境用压缩的js和css <environment includ ...
- 把旧系统迁移到.Net Core 2.0 日记(4) - 使用EF+Mysql
因为Mac 不能装SqlServer, 所以把数据库迁移到MySql,然后EntityFramework要改成Pomelo.EntityFrameworkCore.MySql 数据库迁移时,nvarc ...
- 把旧系统迁移到.Net Core 2.0 日记(3) - 详解依赖注入 (转)
关于DI 依赖注入, 转载这篇文章, 写得很好的. ----------------------------- DI在.NET Core里面被提到了一个非常重要的位置, 这篇文章主要再给大家普及一下关 ...
随机推荐
- JDBC连接数据库的简单介绍
休息10天后重新看了下jdbc,开始振作继续学习(休息10天主要是因为驾照考试太累,2333),希望自己能够调整好心态,继续对程序有着一如既往的喜爱(加油) Connection con=null; ...
- 懒懒的Rain的寒假小结
快开学了,才发现这个寒假算是又废了,放假前满满的雄心壮志要刷多少多少题回家写一会都不行了,唉,在家真不适合学习.可能还是因为没有学习的气氛吧,在家老是就自己一个人,遇到问题或者出现什么错误了没有人可以 ...
- cin 与 getchar 中的坑
今天在一道题上发现一个坑. 输入三个字符,按以下规则求其平均值. (1)如果是数字0~9,那么直接参与求值: (2)如果是其他字符,则其ASCII码参与求值. 输入 输入数据有多组.第一行是数据的组数 ...
- 解决在Vue项目中时常因为代码缩进导致页面报错的问题
前言 如果我们初次使用vue-cli来构建单页SPA应用,在撸代码的过程中有可能会遇到这种因为代码缩进导致 页面报错的问题,导致我们烦不胜烦.接下来我们就来看一看如何解决这个小问题... erro原因 ...
- 让browserify接收命令行参数,在打包时parse yml配置文件
功能需求: 1用browserify把各种js打包成浏览器端的1个bundle.js,含有yml配置文件 约束: 1 yml配置文件不在当前工程里(现在还不知道放哪里,以后也会变),希望在打包时,用命 ...
- MySQL学习(十)
要做:商城的留言板 一般情况,做留言板的显示很容易,直接select查询,再显示出来,但eschop中的留言板难点在于留言数据来自2张表,feedback表和comment表,我们需要把两张表中的内容 ...
- Django 基础介绍
Django 介绍 Python下有许多款不同的 Web 框架.Django是重量级选手中最有代表性的一位.许多成功的网站和APP都基于Django. Django是一个开放源代码的Web应用框架,由 ...
- python实战小程序之购物车
# Author:南邮吴亦凡 # 商品列表 product_list = [ ('Iphone',5800), # 逗号一定不可以省略! ('Mac',4800), ('smartphone',400 ...
- Spring 拦截器实现+后台原理(MethodInterceptor)
MethodInterceptor MethodInterceptor是AOP项目中的拦截器(注:不是动态代理拦截器),区别与HandlerInterceptor拦截目标时请求,它拦截的目标是方法. ...
- induced pluripotent stem cell (iPSC) 诱导性多能干细胞
参考: 诱导性多能干细胞 Induced pluripotent stem cell Induced Pluripotent Stem Cells: Problems and Advantages w ...