[英]Asp.net Core 2.0 Custom Cookie based authentication
我已經從asp.net core 1.0升級到asp.net core 2.0,我需要基於URL的身份驗證來創建授權的cookie。 沒有登錄頁面。 如果url包含某些令牌,那么如果不將其重定向到錯誤頁面,我需要對請求進行身份驗證。 我陷入了重定向循環。 我的代碼有什么問題
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);
});
配置方法
app.UseAuthentication();
app.ValidateRequest(Configuration);
在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()
{
}
}
登錄在http/localhost
上正常運行,但是一旦在https/subdomain.domain.com
上登錄則無法正常工作。 改變是為了做到這一點
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.