在ASP.NET 5中获取AuthenticationProperties [英] Get AuthenticationProperties in ASP.NET 5
问题描述
在ASP.NET 5 MVC 6 RC1中,如何从控制器或过滤器中检索AuthenticationProperties
? HttpContext.Authentication
似乎没有此功能.
In ASP.NET 5 MVC 6 RC1 how do I retrieve AuthenticationProperties
from within a controller or from a filter? HttpContext.Authentication
does't seem to have this functionality.
我考虑过要注册一个CookieAuthenticationEvents.OnValidatePrincipal
处理程序,然后在CookieValidatePrincipalContext
参数上使用Properties
属性.然后,我可以将那些AuthenticationProperties
存储在请求缓存中,以便以后可以获取诸如IssuedUtc
之类的东西.
I thought about registering an CookieAuthenticationEvents.OnValidatePrincipal
handler and then using the Properties
property on the CookieValidatePrincipalContext
argument. Then I could store those AuthenticationProperties
in the request cache so that later I'm able to get things like IssuedUtc
.
是否有更好的解决方案,不需要我自己存储?
Is there a better solution where I don't need to store this myself?
我没有使用ASP.NET Identity,而是将cookie中间件作为独立软件使用.
I'm not using ASP.NET Identity but the cookie middleware as standalone.
推荐答案
在ASP.NET 5中,检索身份验证属性有些麻烦,因为必须实例化AuthenticateContext
:
In ASP.NET 5, retrieving the authentication properties is a bit cumbersome as it must be done by instantiating an 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);
这篇关于在ASP.NET 5中获取AuthenticationProperties的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!