把旧系统迁移到.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里面被提到了一个非常重要的位置, 这篇文章主要再给大家普及一下关 ...
随机推荐
- ECMAScript6语法重点(一)
一. let和const ①let声明的变量只在它的块作用域有效({ }括起来) ②let不能重复声明同一变量 ③const声明的常量不能改(但对象可以加属性) ④const也有块作用域概念 ⑤con ...
- Java中的异常 Exceptions
1. 概念 exception是“exceptional event”的缩写,是指执行程序中发生的事件,破坏了程序的正常执行流程.Java 异常处理机制使程序更加健壮易于调试,它可以告诉程序员三个问题 ...
- 离线人脸识别C#类库分享 虹软2.0版本
目前只封装了人脸检测部分的类库,供大家交流学习,肯定有问题,希望大家在阅读使用的时候及时反馈,谢谢!使用虹软技术开发完成 戳这里下载SDKgithub:https://github.com/dayAn ...
- Java代理机制之初见(理解及实现)
都知道Java中的Spring,有一重要思想:AOP,实现原理也就是Java的动态代理机制.初见代理这个名词时,觉得生活中常有代理的这一说法. 那么,在Java中,代理又是什么呢?它又是如何实现的?实 ...
- 第 8 章 容器网络 - 061 - flannel 的连通与隔离
flannel 的连通与隔离 测试 bbox1 和 bbxo2 的连通性: bbox1 能够 ping 到位于不同 subnet 的 bbox2,通过 traceroute 分析一下 bbox1 到 ...
- Axure XMind整理交互思路
本部分主要是为了研究Xmind思维导图总结设计原型的思路
- Linux ----> debian中环境的常见配置
环境: (debian 9)/(lubuntu18.0.4+virtualbox) 1.配置下载源: gedit /etc/apt/sources.list #163 deb http://mirro ...
- 比较Class.getResource与Class.getClassLoader().getResource两种方式读取资源文件
/** * @author zhangboqing * @date 2018/7/10 */ public class FileDemo { public static void main(Strin ...
- Children's Game UVA - 10905
看90,956这样的串,在比较完之前,就确定大小的,必定选大的放在前.而x=98,y=980;这样的,比较x+y和y+x的大小.如果x+y更小,y就放前. #include <iostream& ...
- 腾讯tOS死亡或注定,为何国内无自主ROM?
http://tech.sina.com.cn/roll/2017-06-26/doc-ifyhmtrw4006354.shtml 腾讯OS死亡或注定,为何国内无自主ROM? 2017年06月26日 ...