在ASP.NET 5中获取AuthenticationProperties

use*_*754 9 asp.net asp.net-core-mvc asp.net-core

在ASP.NET 5 MVC 6 RC1中如何AuthenticationProperties从控制器或过滤器中检索?HttpContext.Authentication似乎没有这个功能.

我考虑过注册一个CookieAuthenticationEvents.OnValidatePrincipal处理程序然后PropertiesCookieValidatePrincipalContext参数上使用该属性.然后我可以将它们存储AuthenticationProperties在请求缓存中,以便以后我能够得到类似的东西IssuedUtc.

有没有更好的解决方案,我不需要自己存储?

我没有使用ASP.NET身份,而是使用cookie中间件作为独立的.

Kév*_*let 8

在ASP.NET 5中,检索身份验证属性有点麻烦,因为必须通过实例化来完成AuthenticateContext:

var context = new AuthenticateContext("[your authentication scheme]");
await HttpContext.Authentication.AuthenticateAsync(context);

if (context.Principal == null || context.Properties == null) {
    throw new InvalidOperationException("The request is not authenticated.");
}

var properties = new AuthenticationProperties(context.Properties);
Run Code Online (Sandbox Code Playgroud)

  • 在.Net Core 2中,当您避开已弃用的Authentication类时,它会变得更短.用var context = await HttpContext.AuthenticateAsync("[your authentication scheme]")替换前两行; (5认同)

Kah*_*azi 3

AuthenticationProperties可以通过 访问IAuthenticateResultFeature。它在 AuthN 和 AuthZ 中间件中设置。

HttpContext.Features.Get<IAuthenticateResultFeature>().AuthenticateResult.Properties
Run Code Online (Sandbox Code Playgroud)