IdentityServer4搭建和使用

烈酒焚心 提交于 2019-11-27 02:21:37

1.NuGet引用 

IdentityServer4.AccessTokenValidation

2.Startup.cs注册

services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
             .AddCookie(options =>
             {
                 options.ExpireTimeSpan = TimeSpan.FromMinutes(60); //到期时间
                 options.Cookie.Name = "lym.Cookies";  //名称

             });

3.注册中件间

app.UseAuthentication();

4.使用细节

var identity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme);//获取实例
            identity.AddClaim(new Claim(ClaimTypes.Sid, "Sid"));//添加 
            identity.AddClaim(new Claim(ClaimTypes.Name, "Name"));//添加 
            await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(identity));//保存
            var sid = HttpContext.User.Claims.SingleOrDefault(s => s.Type == ClaimTypes.Sid);//获取值
            var name = HttpContext.User.Claims.SingleOrDefault(s => s.Type == ClaimTypes.Name);

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!