【发布时间】:2018-07-19 09:27:45
【问题描述】:
我已经从 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()
{
}
}
【问题讨论】:
-
我以此为基础,但有些东西不起作用
-
您在等待登录吗?您提供的代码没有显示。提供可用于重现问题的minimal reproducible example,以便更好地理解所询问的内容。
-
OK cookie 至少有 2 个步骤。您进行身份验证,然后在响应中传回 cookie。然后在后续请求中使用该 cookie。在您的示例中,您尝试设置 cookie 并在同一请求中使用它
-
你能举个例子吗?
标签: asp.net .net asp.net-core .net-core asp.net-core-2.0