Код: Выделить всё
// In building I use
.AddJwtBearer(async opt =>
{
opt.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidIssuer = jwtSection.Issuer,
ValidateAudience = true,
ValidAudience = jwtSection.Audience,
ValidateLifetime = true,
IssuerSigningKeys = keys
};
opt.Events = new JwtBearerEvents
{
OnMessageReceived = context =>
{
if (context.Request.ContentType == "application/x-www-form-urlencoded")
{
if (context.Request.Form.Any(f => f.Key == jwtSection.TokenField))
{
var token = context.Request.Form[jwtSection.TokenField];
// Set jwt cookie
context.Response.Cookies.Append("jwt", token, new CookieOptions
{
HttpOnly = true,
Secure = true,
SameSite = SameSiteMode.None
});
context.Token = token;
}
}
if (context.Request.Cookies.TryGetValue("token", out var jwt))
{
context.Token = jwt;
}
return Task.CompletedTask;
}
};
});
// In startup I use this
app.UseCookiePolicy(new CookiePolicyOptions
{
HttpOnly = Microsoft.AspNetCore.CookiePolicy.HttpOnlyPolicy.Always,
Secure = CookieSecurePolicy.SameAsRequest,
MinimumSameSitePolicy = SameSiteMode.None,
});
app.MapGet("vue/api/me", (HttpContext context) =>
{
return new { name = context.User.Claims.FirstOrDefault(f => f.Type == "name") };
}).RequireAuthorization();
app.MapFallbackToFile("/index.html").RequireAuthorization();
Код: Выделить всё
about:blank
Вызов /me также не авторизован.
Есть идеи, как сохранить токен JWT в клиентском приложении и использовать его для будущей аутентификации? Нет необходимости использовать ответный файл cookie, мне нужно аутентифицировать свое приложение Vue.js, чего бы это ни стоило...
В потоке LTI они вызывают мой бэкэнд в этом действии:< /p>
Код: Выделить всё
[HttpPost("launch")]
[Authorize]
[Consumes("application/x-www-form-urlencoded")]
public async Task PostLaunch([FromForm] JwtPayload request)
{
await Task.CompletedTask;
if (HttpContext.User.Identity?.IsAuthenticated == true)
{
return this.RedirectPreserveMethod("/");
}
}
Подробнее здесь: https://stackoverflow.com/questions/793 ... entication
Мобильная версия