实现最简单的认证,类似之前的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. JavaSE习题 第七章 常用实用类

    问答题 1.怎样实例化一个Calendar对象? Calendar ca=Calendar.getInstance(); 2.Calendar对象调用set(1949,9,1)设置的年月日分别是多少? ...

  2. CSS 控制鼠标在元素停留的样式

    以下资料来自网络,收藏学习总结用: 有时候需要改变鼠标样式,DIV 可以改成手型等,A也可以改成光标形式 巧合要用到鼠标样式效果,就顺便整理了下十五种CSS鼠标样式,小例子供大家使用啊.CSS鼠标样式 ...

  3. 2018年浙江理工大学程序设计竞赛校赛 Problem I: 沙僧

    沙僧 思路: dfs序+差分数组 分层考虑,通过dfs序来查找修改的区间段,然后用差分数组修改 代码: #include<bits/stdc++.h> using namespace st ...

  4. lua --- dofile、loadfile、require

    loadfile: 1>编译代码成中间码并且返回编译后的chunk作为一个函数,而不执行代码 2>在发生错误的情况下,不会抛出异常,但会返回nil和错误信息 3>运行一个文件多次的话 ...

  5. 微信小程序地图demo完整

    <block wx:for="{{data_2}}" wx:key='index' wx:if="{{data_2.length}}"> <v ...

  6. Pascal's Triangle 2(leetcode java)

    问题描述: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return ...

  7. Python while循环实现重试

    try: pass#要执行的代码 except: 状态=True while 状态==True: try: winsound.Beep(800, 1000)#报警提示音 循环=300 while 循环 ...

  8. 第一阶段——站立会议总结DAY07

    未做,有一些作业比较赶,所以,先搁置了它.

  9. 【PowerDesigner】【1】简单介绍

    正文: 创建表格 File→New Model→(Model types; Physical Data Model; Physical Diagram)Model name:名称:DBMS:数据库类型 ...

  10. 关于react16.4——错误边界

    过去,组件内的 JavaScript 错误常常会破坏 React 内部状态,并导致它在下一次渲染时产生神秘的错误.这些错误总会在应用代码中较早的错误引发的,但 React 并没有提供一种方式能够在组件 ...