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

  1. app.UseCookieAuthentication(new CookieAuthenticationOptions()
  2. {
  3. AuthenticationScheme = "IdeaCoreUser",
  4. LoginPath = new PathString("/Login/Login/"),
  5. AccessDeniedPath = new PathString("/Account/Forbidden/"),
  6. AutomaticAuthenticate = true,
  7. AutomaticChallenge = true,
  8. CookieDomain=""
  9. });

2、登录

  1. var claims = new List<Claim> {
  2. new Claim("FullName", customer.Username,ClaimValueTypes.String),
  3. new Claim("Role", "注册用户",ClaimValueTypes.String),
  4. };
  5. var userIdentity = new ClaimsIdentity(claims, "Customer");
  6. var userPrincipal = new ClaimsPrincipal(userIdentity);
  7. HttpContext.Authentication.SignInAsync("IdeaCoreUser", userPrincipal,
  8. new AuthenticationProperties
  9. {
  10. ExpiresUtc = DateTime.UtcNow.AddMinutes(),
  11. IsPersistent = false,
  12. AllowRefresh = false
  13. });

3、退出登录

  1. HttpContext.Authentication.SignOutAsync("IdeaCoreUser");

4、判断是否已经登录

  1. var bol =HttpContext.User.Identity.IsAuthenticated;

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

  1. public static class IdentityExtension
  2. {
  3. public static string FullName(this IIdentity identity)
  4. {
  5. var claim = ((ClaimsIdentity)identity).FindFirst("FullName");
  6. return (claim != null) ? claim.Value : string.Empty;
  7. }
  8. public static string Role(this IIdentity identity)
  9. {
  10. var claim = ((ClaimsIdentity)identity).FindFirst("Role");
  11. return (claim != null) ? claim.Value : string.Empty;
  12. }
  13. }
  1. 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. HOOK(钩子)函数

    安装.卸载钩子的相关函数 钩子类型 按功能分: 1.WH_CALLWNDPROC和WH_CALLWNDPROCRET:使你可以监视发送到窗口过程的消息3.WH_DEBUG    调试钩子4.WH_FO ...

  2. class.forname()方法的学习(转)

    Class.forName(xxx.xx.xx) 返回的是一个类 首先你要明白在java里面任何class都要装载在虚拟机上才能运行.这句话就是装载类用的(和new 不一样,要分清楚). 至于什么时候 ...

  3. python 算法练习

    根据给定的线性函数来确定函数的表达形式: examples: get_function([0,1,2,3,4]) => f(x)=x get_function([1,4,7,10,13]) =& ...

  4. GZIP压缩、解压缩工具类

    GZIP压缩.解压缩工具类: public class GZIPUtiles { public static String compress(String str) throws IOExceptio ...

  5. get与post请求

    1.post请求 如果表单以Post方式提交过来的,接收时必须以Requert.Form来接收,并且表单元素必须有name属性,而Form指定的键的名称就是name属性的值 <form meth ...

  6. Redis 使用 Eval 多个键值自增操作示例

    在PHP上使用Redis 给多个键值进行自增,示例如下: $set['money'] = $this->redis->hIncrByFloat($key, $hour .'_money', ...

  7. CodeForces 685B Kay and Snowflake

    树的重心,树形$dp$. 记录以$x$为$root$的子树的节点个数为$sz[x]$,重儿子为$son[x]$,重心为$ans[x]$. 首先要知道一个结论:以$x$为$root$的子树的重心$ans ...

  8. 《JavaScript网页经典特效300例》

    <JavaScript网页经典特效300例> 基础篇 进阶篇 高级篇

  9. Java 内部类 this

    内部类访问外部类的一个例子: public class Abc { private class Bc { public void print() { System.out.println(Abc.th ...

  10. ES 6 : 字符串的扩展

    1. 字符的Unicode表示法 JavaScript允许表示\u0000—\uFFFF之间的字符.超出这个范围,必须用2个双字节的形式表达.如:"\u20BB7"是汉字 &quo ...