首页 > Web开发 > 详细

net core 3.1 Authorize

时间:2020-06-18 13:07:54      阅读:99      评论:0      收藏:0      [点我收藏+]
net core 3.1  Authorize
1
//配置authorrize
services.AddAuthentication(b =>
            {
                b.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                b.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                b.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            }).
            AddCookie(b =>
            {
    //登陆地址
    b.LoginPath = "/login";
    //sid
    b.Cookie.Name = "My_SessionId";
    // b.Cookie.Domain = "shenniu.core.com";
    b.Cookie.Path = "/";
    b.Cookie.HttpOnly = true;
    //b.Cookie.Expiration = new TimeSpan(0, 0, 30);

    b.ExpireTimeSpan = new TimeSpan(0, 0, 30);
});

2.
Configure
//启用身份认证,启用后可以在Controller中使用User API
app.UseAuthentication();

3.

   string name = "罗分明";
//登陆授权
var claims = new List<Claim>();
claims.Add(new Claim(ClaimTypes.Name, name));
                var indentity = new ClaimsIdentity(claims, "denglu");
var principal = new ClaimsPrincipal(indentity);
await HttpContext.SignInAsync(
    CookieAuthenticationDefaults.AuthenticationScheme,
    principal);
                //验证是否授权成功
                if (principal.Identity.IsAuthenticated)
                {
                    return Redirect("/home/index");
                }

4.
[Authorize(AuthenticationSchemes = CookieAuthenticationDefaults.AuthenticationScheme)]

var a = User.Identity.Name;

 

net core 3.1 Authorize

原文:https://www.cnblogs.com/LiuFengH/p/13156803.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!