实现最简单的认证,类似之前的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方法还没实现,会出现错误

PlatformNotSupportedException: Operation is not supported on this platform.

解决方法, 要添加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的更多相关文章

  1. 把旧系统迁移到.Net Core 2.0 日记 (18) --JWT 认证(Json Web Token)

    我们最常用的认证系统是Cookie认证,通常用一般需要人工登录的系统,用户访问授权范围的url时,会自动Redirect到Account/Login,登录后把认证结果存在cookie里. 系统只要找到 ...

  2. 把旧系统迁移到.Net Core 2.0 日记 (15) --Session 改用Redis

    安装Microsoft.Extensions.Caching.Redis.Core NuGet中搜索Microsoft.Extensions.Caching.Redis.Core并安装,此NuGet包 ...

  3. 把旧系统迁移到.Net Core 2.0 日记(1) - Startup.cs 解析

    因为自己到开发电脑转到Mac Air,之前的Webform/MVC应用在Mac 跑不起来,而且.Net Core 2.0 已经比较稳定了. 1. 为什么会有跨平台的.Net Core  近年来,我们已 ...

  4. 把旧系统迁移到.Net Core 2.0 日记 (12) --发布遇到的问题

    1. 开发时是在Mac+MySql, 尝试发布时是在SQL2005+Win 2008 (第一版) 在Startup.cs里,数据库连接要改,分页时netcore默认是用offset关键字分页, 如果用 ...

  5. 把旧系统迁移到.Net Core 2.0 日记 (17) --多租户和SoftDelete

    在EF Core 2.0版本中出现了全局过滤新特性即HasQueryFilter,它出现的意义在哪里?能够解决什么问题呢? 通过HasQueryFilter方法来创建过滤器能够允许我们对访问特定数据库 ...

  6. 把旧系统迁移到.Net Core 2.0 日记(10) -- EF core 和之前版本多对多映射区别

    EF Core 现在不支持多对多映射,只能做2个一对多映射. 比如Product和Category 我现在定义Product和Category是多对多关系. 那么实体定义如下: public clas ...

  7. 把旧系统迁移到.Net Core 2.0 日记(8) - EASYUI datagrid+ Dapper+ 导出Excel

    迁移也没太大变化,有一个, 之前的Request.QueryString 是返回NameValueCollection, 现在则是返回整个字符串. 你要改成Request.Query[“key”] 直 ...

  8. 把旧系统迁移到.Net Core 2.0 日记(5) Razor/HtmlHelper/资源文件

    net core 的layout.cshtml文件有变化, 区分开发环境和非开发环境. 开发环境用的是非压缩的js和css, 正式环境用压缩的js和css <environment includ ...

  9. 把旧系统迁移到.Net Core 2.0 日记(4) - 使用EF+Mysql

    因为Mac 不能装SqlServer, 所以把数据库迁移到MySql,然后EntityFramework要改成Pomelo.EntityFrameworkCore.MySql 数据库迁移时,nvarc ...

  10. 把旧系统迁移到.Net Core 2.0 日记(3) - 详解依赖注入 (转)

    关于DI 依赖注入, 转载这篇文章, 写得很好的. ----------------------------- DI在.NET Core里面被提到了一个非常重要的位置, 这篇文章主要再给大家普及一下关 ...

随机推荐

  1. spring boot 配置双数据源mysql、sqlServer

    背景:原来一直都是使用mysql数据库,在application.properties 中配置数据库信息 spring.datasource.url=jdbc:mysql://xxxx/test sp ...

  2. springboot 启动报错 java.lang.IllegalStateException: Failed to introspect annotated methods on class org

    . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ...

  3. Maven命令行创建java或javaWeb项目

    Maven命令行创建java或javaWeb项目   1.命令行创建普通java项目 mvn archetype:generate -DgroupId=com.fxust -DartifactId=d ...

  4. 《剑指offer》第六十七题(把字符串转换成整数)

    // 面试题67:把字符串转换成整数 // 题目:请你写一个函数StrToInt,实现把字符串转换成整数这个功能.当然,不 // 能使用atoi或者其他类似的库函数. #include <ios ...

  5. tips 移入悬浮功能

    前景: 页面部分区域需要移入悬浮效果,当然默认的 title 也是可以的,最多只是格格不入,但是却是最为靠谱的.. 思路: 基于 jq 实例扩展 .使用立即执行函数保持功能独立. 自定义类实现功能封装 ...

  6. python的try finally (还真不简单)

    https://www.cnblogs.com/cotton/p/3785999.html def f(): try: print 1 return 1 finally: print 0 return ...

  7. SVN-版本控制工具安装与使用

    什么是版本控制? 版本控制(Revision control)是一种软体工程技巧,籍以在开发的过程中,确保由不同人所编辑的同一档案都得到更新. 版本控制透过文档控制(documentation con ...

  8. pseudotime专题

    review:Computational Methods for Trajectory Inference from Single-Cell Transcriptomics Tools/Algorit ...

  9. 三个解释——MVC的网址

    [三个MVC网址]1.https://www.cnblogs.com/sunniest/p/4555801.html2.https://www.cnblogs.com/wmyskxz/p/884846 ...

  10. 解决IIS7虚拟目录出现HTTP 错误 500.19(由于权限不足而无法读取配置文件)的问题

    今天在windows 7笔记本上搭建一个ASP.NET网络平台,启动IIS 7 时候出现错误: 错误摘要HTTP 错误 500.19 - Internal Server Error无法访问请求的页面, ...