简体   繁体   English

基于Asp.net Core 2.0自定义Cookie的身份验证

[英]Asp.net Core 2.0 Custom Cookie based authentication

I have upgraded from asp.net core 1.0 to asp.net core 2.0 I need url based authentication which create a authorized cookie. 我已经从asp.net core 1.0升级到asp.net core 2.0,我需要基于URL的身份验证来创建授权的cookie。 There is no Login page. 没有登录页面。 If url contains certain token I need to authenticate the request if not redirect them to error page. 如果url包含某些令牌,那么如果不将其重定向到错误页面,我需要对请求进行身份验证。 I am stuck in redirect loop. 我陷入了重定向循环。 what's wrong in my code 我的代码有什么问题

ConfigureServices method ConfigureServices方法

 services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
                .AddCookie(options =>
                {
                    options.LoginPath = new PathString("/Error/");
                    options.AccessDeniedPath = new PathString("/Error/");
                    options.SlidingExpiration = true;
                    options.ExpireTimeSpan = TimeSpan.FromMinutes(20);

                });

Configure Method 配置方法

app.UseAuthentication();
app.ValidateRequest(Configuration);

In validaterequest middleware 在validaterequest中间件中

public Task Invoke(HttpContext context)
        {
                context.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme,
                   principal,
                   new AuthenticationProperties
                   {
                       ExpiresUtc = DateTime.UtcNow.AddSeconds(expiration),
                       IsPersistent = true,
                       AllowRefresh = true,
                       IssuedUtc = DateTime.UtcNow,
                   });
return _next.Invoke(context);
}


[MiddlewareFilter(typeof(validaterequestPipeline))]
    public class HomeController : Controller
    {
      [Authorize]
        [HttpGet]
        public IActionResult Index()
        {
        }
   }

Login was working properly on http/localhost but once it is on https/subdomain.domain.com it didn't work. 登录在http/localhost上正常运行,但是一旦在https/subdomain.domain.com上登录则无法正常工作。 Change was to do this 改变是为了做到这一点

services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie(options =>
{
    options.LoginPath = new PathString("/account/signin");
    options.SlidingExpiration = true;
    options.ExpireTimeSpan = TimeSpan.FromMinutes(20);
    options.Cookie.SameSite = SameSiteMode.None;
});

options.Cookie.SameSite = SameSiteMode.None;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM