asp.net - 在ASP.NET 5中获取AuthenticationProperties

标签 asp.net asp.net-core asp.net-core-mvc

在ASP.NET 5 MVC 6 RC1中,如何从 Controller 内部或过滤器中检索AuthenticationPropertiesHttpContext.Authentication似乎没有此功能。

我考虑过注册一个CookieAuthenticationEvents.OnValidatePrincipal处理程序,然后在Properties参数上使用CookieValidatePrincipalContext属性。然后,我可以将这些AuthenticationProperties存储在请求缓存中,以便以后可以获取类似IssuedUtc的内容。

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

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

最佳答案

在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);

关于asp.net - 在ASP.NET 5中获取AuthenticationProperties,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34534892/

相关文章:

sql-server - 在 .NET Core 中的连接字符串之间轻松切换

asp.net-core - .NET Core MVC 页面更改后不刷新

c# - Asp.net core cookie 身份验证请求过多

asp.net - session 中存储信用卡信息的是 'ok'吗?

c# - 为什么我的自定义用户控件的子控件没有被初始化?

c# - C# 中没有智能感知

c# - 如何使用工厂方法的返回值在启动类中添加单例

c# - 使用 MVC Controller 重载

asp.net - .NET Core 静态文件响应压缩中间件

asp.net - 在 Akka.NET 中同步访问 ActorSystem 外部对象的最佳方法是什么?