csharp Cookie身份验证

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp Cookie身份验证相关的知识,希望对你有一定的参考价值。

[Route("")]
    public class HomeController:Controller
    {
        [HttpGet]
        public IActionResult Index()
        {
            return View();
        }

        [Route("forbidden")]
        public IActionResult Forbidden()
        {
            return View();
        }

        [HttpGet("private")]
        [Authorize]
        public IActionResult Private()
        {
            return View();
        }
    }
[Route("account")]
    public class AccountController:Controller
    {
        [HttpGet("signin")]
        public async Task<IActionResult> SignIn()
        {
            var claims = new List<Claim>
            {
                new Claim(ClaimTypes.Name,"My Name",ClaimValueTypes.String)
            };
            ClaimsIdentity identity = new ClaimsIdentity(claims,"Custom");
            ClaimsPrincipal principal = new ClaimsPrincipal(identity);
            await HttpContext.Authentication.SignInAsync("OurAuthenticationCookie", principal);
            var returnUrl = Request.Query["ReturnUrl"];
            return Redirect($"../../{returnUrl}");
        }
    }
<PackageReference Include="Microsoft.AspNetCore.Authentication.Cookies" Version="1.1.2" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.3" />

var cookieOptions = new CookieAuthenticationOptions()
            {
                AuthenticationScheme="OurAuthenticationCookie",
                LoginPath="/account/signin",
                AccessDeniedPath="/forbidden", //page to show when authorize fails
                AutomaticAuthenticate=true,
                AutomaticChallenge=true,
                CookieHttpOnly=true
            };


            app.UseCookieAuthentication(cookieOptions);

以上是关于csharp Cookie身份验证的主要内容,如果未能解决你的问题,请参考以下文章

.NET 基于 cookie 的身份验证,无需表单身份验证

Cookie 与基本身份验证

csharp 键入模式身份验证

您是不是更改 cookie 身份验证用户的身份验证令牌?如果有,多久一次?

Web 身份验证状态 - 会话与 Cookie?

.Net SignalR 在还配置了 Cookie 身份验证时使用 JWT 承载身份验证