当前位置: 移动技术网 > IT编程>开发语言>.net > asp.net core中使用cookie身份验证

asp.net core中使用cookie身份验证

2019年12月02日  | 移动技术网IT编程  | 我要评论

3u8514航班,新道亚里沙,资本前瞻

配置

在 startup.configureservices 方法中,创建具有 addauthentication 和 addcookie 方法的身份验证中间件服务:

services.addauthentication(microsoft.aspnetcore.authentication.cookies.cookieauthenticationdefaults.authenticationscheme)
            .addcookie(options =>
            {
                // cookie settings
                options.cookie.httponly = true;
                options.expiretimespan = timespan.fromminutes(20);

                options.loginpath = "/account/login";
                options.accessdeniedpath = "/account/accessdenied";
                options.slidingexpiration = true;
            });

 

authenticationscheme 传递到 addauthentication 设置应用程序的默认身份验证方案。 如果有多个 cookie 身份验证实例,并且你想要使用特定方案进行授权,authenticationscheme 会很有用。 将 authenticationscheme 设置为cookieauthenticationdefaults。 authenticationscheme为方案提供值 "cookie"。 可以提供任何用于区分方案的字符串值。
应用的身份验证方案不同于应用的 cookie 身份验证方案。 如果未向 addcookie提供 cookie 身份验证方案,则使用 cookieauthenticationdefaults.authenticationscheme ("cookie")。
默认情况下,身份验证 cookie 的 isessential 属性设置为 true。 当站点访问者未同意数据收集时,允许使用身份验证 cookie。 


在 startup.configure中,调用 useauthentication 和 useauthorization 设置 httpcontext.user 属性,并为请求运行授权中间件。 调用 useendpoints之前调用 useauthentication 和 useauthorization 方法:

app.usecookiepolicy();
app.useauthentication(); app.useauthorization(); app.useendpoints(endpoints => {   endpoints.mapcontrollers();   endpoints.maprazorpages(); });

 

登录

若要创建保存用户信息的 cookie,请构造一个 claimsprincipal。 将对用户信息进行序列化并将其存储在 cookie 中。
使用任何所需的 claim创建 claimsidentity,并调用 signinasync 以登录用户:

var claims = new list<claim>
{
new claim(claimtypes.name, user.email),
new claim("fullname", user.fullname),
new claim(claimtypes.role, "administrator"),
};

var claimsidentity = new claimsidentity(
claims, cookieauthenticationdefaults.authenticationscheme);

var authproperties = new authenticationproperties
{
//allowrefresh = <bool>,
// refreshing the authentication session should be allowed.

//expiresutc = datetimeoffset.utcnow.addminutes(10),
// the time at which the authentication ticket expires. a 
// value set here overrides the expiretimespan option of 
// cookieauthenticationoptions set with addcookie.

//ispersistent = true,
// whether the authentication session is persisted across 
// multiple requests. when used with cookies, controls
// whether the cookie's lifetime is absolute (matching the
// lifetime of the authentication ticket) or session-based.

//issuedutc = <datetimeoffset>,
// the time at which the authentication ticket was issued.

//redirecturi = <string>
// the full path or absolute uri to be used as an http 
// redirect response value.
};

await httpcontext.signinasync(
cookieauthenticationdefaults.authenticationscheme, 
new claimsprincipal(claimsidentity), 
authproperties);

 

signinasync 创建加密的 cookie,并将其添加到当前响应中。 如果未指定 authenticationscheme,则使用默认方案。
asp.net core 的数据保护系统用于加密。 对于托管在多台计算机上的应用程序、跨应用程序或使用 web 场进行负载平衡,请将数据保护配置为使用相同的密钥环和应用程序标识符。

 

注销

 

若要注销当前用户并删除其 cookie,请调用 signoutasync:

await httpcontext.signoutasync(cookieauthenticationdefaults.authenticationscheme);

 

如果 cookieauthenticationdefaults.authenticationscheme (或 "cookie")不用作方案(例如 "contosocookie"),请提供配置身份验证提供程序时所使用的方案。 否则,将使用默认方案。

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网