1、在 app.UseMvc 前面加上app.UseCookieAuthentication

app.UseCookieAuthentication(new CookieAuthenticationOptions()
{
AuthenticationScheme = "IdeaCoreUser",
LoginPath = new PathString("/Login/Login/"),
AccessDeniedPath = new PathString("/Account/Forbidden/"),
AutomaticAuthenticate = true,
AutomaticChallenge = true,
CookieDomain=""
});

2、登录

var claims = new List<Claim> {
new Claim("FullName", customer.Username,ClaimValueTypes.String),
new Claim("Role", "注册用户",ClaimValueTypes.String),
};
var userIdentity = new ClaimsIdentity(claims, "Customer");
var userPrincipal = new ClaimsPrincipal(userIdentity);
HttpContext.Authentication.SignInAsync("IdeaCoreUser", userPrincipal,
new AuthenticationProperties
{
ExpiresUtc = DateTime.UtcNow.AddMinutes(),
IsPersistent = false,
AllowRefresh = false
});

3、退出登录

HttpContext.Authentication.SignOutAsync("IdeaCoreUser");

4、判断是否已经登录

var bol =HttpContext.User.Identity.IsAuthenticated;

5、使用IIdentity拓展方法来获取存的值

    public static class IdentityExtension
{
public static string FullName(this IIdentity identity)
{
var claim = ((ClaimsIdentity)identity).FindFirst("FullName");
return (claim != null) ? claim.Value : string.Empty;
}
public static string Role(this IIdentity identity)
{
var claim = ((ClaimsIdentity)identity).FindFirst("Role");
return (claim != null) ? claim.Value : string.Empty;
}
}
var fullname = HttpContext.User.Identity.FullName();

.Net Core Identity外面使用Cookie中间件的更多相关文章

  1. .NET Core 从1.1升级到2.0记录(Cookie中间件踩坑)

    .NET Core 2.0 新时代 万众瞩目的.NET Core 2.0终于发布了,原定于9.19的dotnetconf大会的发布时间大大提前了1个月,.NET Core 2.0/.NET Stand ...

  2. 在ASP.NET Core 中使用Cookie中间件

    在ASP.NET Core 中使用Cookie中间件 ASP.NET Core 提供了Cookie中间件来序列化用户主题到一个加密的Cookie中并且在后来的请求中校验这个Cookie,再现用户并且分 ...

  3. 在ASP.NET Core 中使用Cookie中间件 (.net core 1.x适用)

    在ASP.NET Core 中使用Cookie中间件 ASP.NET Core 提供了Cookie中间件来序列化用户主题到一个加密的Cookie中并且在后来的请求中校验这个Cookie,再现用户并且分 ...

  4. .NET Core 2.0 Cookie中间件 权限验证

    :在ConfigureServices添加Cookie中间件,使用自定义Scheme services.AddAuthentication(options=> { options.Default ...

  5. ASP.NET Core 中的那些认证中间件及一些重要知识点

    前言 在读这篇文章之间,建议先看一下我的 ASP.NET Core 之 Identity 入门系列(一,二,三)奠定一下基础. 有关于 Authentication 的知识太广,所以本篇介绍几个在 A ...

  6. [转]ASP.NET Core 中的那些认证中间件及一些重要知识点

    本文转自:http://www.qingruanit.net/c_all/article_6645.html 在读这篇文章之间,建议先看一下我的 ASP.NET Core 之 Identity 入门系 ...

  7. ASP.NET Core Identity Hands On(2)——注册、登录、Claim

    上一篇文章(ASP.NET Core Identity Hands On(1)--Identity 初次体验)中,我们初识了Identity,并且详细分析了AspNetUsers用户存储表,这篇我们将 ...

  8. ASP.NET Core Identity 实战(2)——注册、登录、Claim

    上一篇文章(ASP.NET Core Identity Hands On(1)--Identity 初次体验)中,我们初识了Identity,并且详细分析了AspNetUsers用户存储表,这篇我们将 ...

  9. ASP.NET Core Identity 实战(3)认证过程

    如果你没接触过旧版Asp.Net Mvc中的 Authorize 或者 Cookie登陆,那么你一定会疑惑 认证这个名词,这太正式了,这到底代表这什么? 获取资源之前得先过两道关卡Authentica ...

随机推荐

  1. css3 box-reflect 倒影效果

    语法: box-reflect:包括3个值. 1. direction 定义方向,取值包括 above . below . left . right. above: 指定倒影在对象的上边 below: ...

  2. Spring in Action --- 使用MockMvc时报异常

    今天在学习spring时模仿了书上的代码编写基于mockmvc的测试用例,但是运行时报 Error:(8, 8) java: 无法访问javax.servlet.ServletException   ...

  3. FZU 2238 Daxia & Wzc's problem

    公式. $a×C_{m + i - 1}^m + d×C_{m + i - 1}^{m + 1}$. 推导过程可以看http://blog.csdn.net/queuelovestack/articl ...

  4. [HMLY]4.CocoaPods详解----制作

    作者:wangzz 原文地址:http://blog.csdn.net/wzzvictory/article/details/20067595 转载请注明出处   学会使用别人的pods依赖库后,你一 ...

  5. erase-credentials配置

    转自:Spring Security怎样不让默认的ProviderManager清除密码等信息 <authentication-manager erase-credentials="f ...

  6. 【2】JavaScript编程全解笔记(二)

    你过去的种种经历,就像人生的一颗颗珍珠,在未来的某一天,你找到了那根线,你就会把她们串联起来,变成美丽的项链. 第八章 客户端 JavaScript 与 HTML 1. 浏览器渲染页面的步骤 2.   ...

  7. SVN 版本控制工具

    1,安装完服务端VisualSVN Server和客户端TortoiseSVN 后,随便在一个文件夹下,右键,会看到有SVN checkout 选项,这个选项只有在第一次在仓库下下载的时候会用到: 1 ...

  8. <hdu - 1280> 前M大的数 (注意其中的细节)

    这是杭电hdu上的链接http://acm.hdu.edu.cn/showproblem.php?pid=1280  Problem Description: 还记得Gardon给小希布置的那个作业么 ...

  9. php 便利数组方法

    数组在PHP中是一个非常强大的武器,用起来方便.容易,由于使用起来异常灵活,用它就可以实现数据结构中的链表.栈.队列.堆以及所谓的字典.集合等,也可以转换成XML格式. 1.使用for for语句遍历 ...

  10. xml文件查找重复元素(超简单版)

    使用WPS,新建一个表格文件,将xml拖入表格,点数据,选中存在重复项的列,点高亮重复项,OK.