把旧系统迁移到.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里面被提到了一个非常重要的位置, 这篇文章主要再给大家普及一下关 ...
随机推荐
- Ubuntu18.04下搭建LAMP环境
一.Apache2 web 服务器的安装 : 可以先更新一下服务器 1.sudo apt-get update # 获取最新资源包 2.sudo apt-get upgrade ...
- 设计模式(八)Dynamic Proxy Parttern 动态代理模式
举例: jdk自带动态代理 javaassit字节码操作库实现 CGLIB ASM底层操作 实际例子: 使用jdk自带动态代理 java.lang.reflect.Proxy 作用 动态生成代理类和对 ...
- 从flask视角理解angular(四)Route
app-routing.modules.ts import { NgModule } from '@angular/core'; import { RouterModule, Routes } fro ...
- 在docker 容器中安装命令
apt-get update ##跟新 //vi apt install vim //weget apt install weget //yum apt install yum //ifconfig ...
- h5做直播的弹幕效果
最近在搞弹幕效果所以就写一个关于弹幕的吧,刚开始寻思去网上找插件的,但找的插件和我的需求都不太相符,其实做弹幕我知道的有两种方法: 1:一种是用canvas和对象来完成弹幕想过,用canvas来完成弹 ...
- spring cloud: Hystrix(八):turbine集群监控(dashboard)
turbine是聚合服务器发送事件流数据的一个工具,hystrix的监控中,只能监控单个节点,实际生产中都为集群, 因此可以通过turbine来监控集群下hystrix的metrics情况,通过eur ...
- ubuntu16.04+cuda8.0+gpu
安装完ubuntu系统之后,在学校的环境下,联网需要安装mentohust. 参考: http://www.cnblogs.com/alexanderkun/p/6905604.html http:/ ...
- sass制作雪碧图
1.配置文件config.rb http_path = "../../../" css_dir = "Content/css" sass_dir = " ...
- MyEclipse6.5的反编译插件的安装
常用的几种反编译工具 1. JD-GUI[推荐] JD-GUI是属于Java Decompiler项目(JD项目)下个的图形化运行方式的反编译器.JD-Eclipse属于Java Decompiler ...
- Pytorch的Reproducibility(可复现性)
对于Pytorch的结果复现,在不同版本的pytorch或者不同的硬件平台之间,不能够保证结果是完全一致的,即使在CPU和GPU上采用相同的随机数种子. 关于torch.backend.cudnn.b ...