黑客24小时在线接单网站

黑客在线接单,网站入侵,渗透测试,渗透网站,入侵网站

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

本文转载自微信公众号「UP技术控」,作者conan5566。转载本文请联系UP微信官方账号技术控制。conan5566

背景

ASP.NET Core Identity 是创建和维护登录名称的完整全功能身份验证程序。cookie 不能使用基于身份验证的程序ASP.NET Core Identity 。

配置

在 Startup.ConfigureServices 创建具有 *** AddAuthentication 和 AddCookie 身份验证中间件服务:

  • services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie();
  • app.UseAuthentication();
  • AuthenticationScheme 传递到 AddAuthentication 为应用程序设置默认身份验证方案。如果有多个 cookie 身份验证实例,您想使用特定的授权方案,AuthenticationScheme 会很有用AuthenticationScheme 设置为CookieAuthenticationDefaults。AuthenticationScheme 为方案提供价值"cookie"。可提供任何用于区分方案的字符串值。

    与应用程序不同的 cookie 身份验证方案。如果不是 AddCookie提供 cookie 使用 身份验证方案CookieAuthenticationDefaults.AuthenticationScheme ("Cookie")。

    默认情况下,身份验证 cookie 的 IsEssential 属性设置为 true。当网站访问者不同意收集数据时,允许使用身份验证 cookie。

    登录

    如果要创建保存用户信息的 cookie,请构造一个 ClaimsPrincipal。序列化用户信息并存储在 cookie 中。

    使用任何需要的 Claim创建 ClaimsIdentity,并调用 SignInAsync 登录用户:

  • ///<summary>
  • ///
  • ///</summary>
  • ///<paramname="model"></param>
  • ///<paramname="returnUrl"></param>
  • ///<returns></returns>
  • [HttpPost]
  • [AllowAttribute]
  • [ValidateAntiForgeryToken]
  • publicasyncTask<IActionResult>Login(LoginModelmodel,stringreturnUrl=null)
  • {
  • if(!ModelState.IsValid)
  • {
  • returnJson(new{state="error",message="数据验证失败"});
  • }
  • stringip=GetRemoteIpAddress();
  • varr=awaitUserApp.SaasLoginAsync(model.Account,model.Password,ip);
  • if(!string.IsNullOrEmpty(r.Error))
  • {
  • returnJson(new{state="error",message=r.Error});
  • }
  • varclaims=newList<Claim>
  • {
  • newClaim(ClaimTypes.UserData,getCurrentUser(r.User,ip).ToString()),
  • };
  • varclaimsIdentity=newClaimsIdentity(
  • claims,CookieAuthenticationDefaults.AuthenticationScheme);
  • varauthProperties=newAuthenticationProperties
  • {
  • ExpiresUtc=DateTimeOffset.Now.AddMinutes(120)
  • };
  • awaitHttpContext.SignInAsync(
  • CookieAuthenticationDefaults.AuthenticationScheme,
  • newClaimsPrincipal(claimsIdentity),
  • authProperties);
  • returnJson(new{state="success",message="登录成功。",returnUrl=RedirectToLocal(returnUrl)});
  • }
  • SignInAsync 创建加密 cookie,并将其添加到当前响应中。如果没有指定 AuthenticationScheme,使用默认方案。

    ASP.NET Core 数据保护系统用于加密。对于托管在多台计算机上的应用程序、跨应用程序或使用 web 场负载平衡,请将数据保护配置为使用相同的密钥环和应用程序标识符。

    注销

    如果要注销当前用户并删除 cookie,请调用 SignOutAsync:

  • ///<summary>
  • ///
  • ///</summary>
  • ///<returns></returns>
  • [HttpPost]
  • [ValidateAntiForgeryToken]
  • publicasyncTask<IActionResult>LogOff()
  • {
  • if(bool.Parse(Configuration.GetSection("IsIdentity").Value))
  • {
  • returnSignOut("Cookies","oidc");
  • }
  • else
  • {
  • if(User.Identity.IsAuthenticated)
  • {
  • stringuserdata=User.Claims.FirstOrDefault(o=>o.Type==ClaimTypes.UserData)?.Value;
  • awaitUserApp.LogOffAsync(CurrentUser.FromJson(userdata));
  • }
  • awaitHttpContext.SignOutAsync(
  • CookieAuthenticationDefaults.AuthenticationScheme);
  • returnRedirectToAction(actionName:nameof(Login),controllerName:"Account");
  • }
  • }
  • 参考资料

    https://docs.microsoft.com/zh-cn/aspnet/core/security/authentication/?view=aspnetcore-5.0

       
    • 评论列表:
    •  嘻友卮留
       发布于 2022-06-02 11:58:09  回复该评论
    • 证实例,您想使用特定的授权方案,AuthenticationScheme 会很有用AuthenticationScheme 设置为CookieAuthenticationDefaults。Authentica
    •  可难烟柳
       发布于 2022-06-02 12:08:08  回复该评论
    • icasyncTask<IActionResult>LogOff(){if(bool.Parse(Configuration.GetSection("IsIdentity").Value)){returnSignOut("Cookies","oidc");}else{
    •  颜于铃予
       发布于 2022-06-02 07:15:08  回复该评论
    • 使用任何需要的 Claim创建 ClaimsIdentity,并调用 SignInAsync 登录用户:///<summary>//////</summary>///<paramname="model"></param>///&

    发表评论:

    Powered By

    Copyright Your WebSite.Some Rights Reserved.