首页 > Web开发 > 详细

ASP.Net Core Cookie 身份验证

时间:2019-08-31 19:09:19      阅读:69      评论:0      收藏:0      [点我收藏+]

创建Cookie身份验证

  • Starup.cs 代码:
    public void ConfigureServices(IServiceCollection services)
    {
        //...

        services.AddAuthentication(options =>
        {
            options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
        })
        .AddCookie();

        //...
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        // ...

        app.UseAuthentication();

        //...
    }
  • AccountController.cs 代码:
    /// <summary>
    /// 登录 
    /// </summary>
    [HttpPost]
    public async Task<IActionResult> Login(string account, string password, string returnUrl)
    {
        //...

        var claimIdentity = new ClaimsIdentity("cookie");
        claimIdentity.AddClaim(new Claim(ClaimTypes.Name, "1"));
        await HttpContext.SignInAsync(
            new ClaimsPrincipal(claimIdentity),
            new AuthenticationProperties { IsPersistent = true });
        
        //...
    }

    /// <summary>
    /// 退出登录
    /// </summary>
    public async Task<IActionResult> Logout()
    {
        await HttpContext.SignOutAsync();
    }

ASP.Net Core Cookie 身份验证

原文:https://www.cnblogs.com/cc1027cc/p/11439990.html

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