zoukankan      html  css  js  c++  java
  • .Net Core Identity外面使用Cookie中间件

    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(20),
            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();
  • 相关阅读:
    Ghost博客安装
    PHP变量作用域
    ssh文件传输命令:sz与rz命令
    excel怎么固定第一行
    memcache和redis区别
    Memcache分布式部署方案
    Memcache服务器端参数说明
    Memcache基础教程
    在Windows下安装Memcached
    MySQL体系结构和存储引擎概述
  • 原文地址:https://www.cnblogs.com/ideacore/p/6282976.html
Copyright © 2011-2022 走看看