Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在AuthenticationProperties中读取MVC OWC?_C#_.net_Asp.net Mvc_Owin - Fatal编程技术网

C# 如何在AuthenticationProperties中读取MVC OWC?

C# 如何在AuthenticationProperties中读取MVC OWC?,c#,.net,asp.net-mvc,owin,C#,.net,Asp.net Mvc,Owin,当用户登录时,我正在设置IsPersistent,如何读取该值 var identity = await UserManager.CreateIdentityAsync(appUser, DefaultAuthenticationTypes.ApplicationCookie); HttpContext.GetOwinContext().Authentication.SignIn(new AuthenticationProperties() { IsPersistent = false }, i

当用户登录时,我正在设置IsPersistent,如何读取该值

var identity = await UserManager.CreateIdentityAsync(appUser, DefaultAuthenticationTypes.ApplicationCookie);
HttpContext.GetOwinContext().Authentication.SignIn(new AuthenticationProperties() { IsPersistent = false }, identity);

由于没有太多的描述,我不确定上下文。无论如何,当您对当前请求执行身份验证时,您可以获得登录调用提供的所有
AuthenticationProperties
。代码应该是

AuthenticateResult authenticateResult = await HttpContext.GetOwinContext().Authentication.AuthenticateAsync(DefaultAuthenticationTypes.ApplicationCookie);

AuthenticationProperties yourAuthenticationProperties = authenticateResult.Properties;
AuthenticateResult authenticateResult = await HttpContext.GetOwinContext().Authentication.AuthenticateAsync(DefaultAuthenticationTypes.ApplicationCookie);

AuthenticationProperties yourAuthenticationProperties = authenticateResult.Properties;

你能试试这个吗,我还没试过

IAuthenticationManager AuthenticationManager
{
  get { return HttpContext.GetOwinContext().Authentication; }
}

正如@Nkosi所说,您可以从OWIN检索信息,因为它没有存储在那里,您需要检索cookie本身并手动解析信息,但为此,您需要一个OWIN中间件,这样您就可以随心所欲地操纵cookie

如果在ConfigureAuth中配置了
CookieAuthOptions
,则可以声明
静态属性CookieAuthOptions
,或者任何其他AuthOption也应实现
ISecureDataFormat
(例如:
OAuthBeareOptions.AccessTokenFormat
)。此方法包含
Protect
Unprotect
方法

无论何时需要访问
AuthenticationProperties
,您都必须能够掌握Owin上下文以获取对实现正确格式的票证的引用

作为一个基本的示例步骤

Startup.cs => public static CookieAuthenticationOptions  CookieAuthOptions; 
Startup.cs => ConfigureAuth => CookieAuthOptions.CookieName = "xxx"; 
BaseController.cs => Startup.CookieAuthOptions.TicketDataFormat.Unprotect(Request.Cookies["xxx"].Value).Properties;
得到你想要的

PS:你没有提到你在哪里需要这个,我以为它会在控制器中


I asume DefaultAuthenticationTypes.ApplicationCookie包含cookie身份验证方案。

AspNet.Identity
允许您访问会话的
IsPersistent
bool
值。读取其值的最直接方法是调用
authenticateSync()

请注意,您需要将其包装在
async
方法中,例如:

@using System.Threading.Tasks;
public async Task<ActionResult> SomeMethodName(...) { //etc }
@使用System.Threading.Tasks;
公共异步任务SomeMethodName(…){//etc}

@MarioDS,这个问题没有引起注意是有原因的。因为这不是很清楚。但抛开这些不谈,你首先应该了解房产是关于什么的。简而言之,检查此答案,因为它(属性)未存储以便读回。它只是指示框架创建cookie。因此,我的假设是,一旦cookie存在,那么属性可能被设置为true,否则将为false。@Nkosi我意识到,在我设置悬赏后不久——不幸的是,没有回头路:)。我真正想知道的是如何读回其他属性(特别是在AuthenticationProperties的“Dictionary”中设置的属性)。也许这个链接会对您有所帮助@nik您使用哪种身份验证?您能解释一下您想在视图中阅读身份验证道具、其他控制器等的更多细节吗?我认为这句话不正确
ispersist
实际上存在于
HttpContext.GetOwinContext
中。您能指出在HttpContext.GetOwinContext中可以找到ispersist的确切位置吗?。。。然后。。。?
@using Microsoft.AspNet.Identity;
var authenticateResult = await HttpContext.GetOwinContext()
                           .Authentication.AuthenticateAsync(
                               DefaultAuthenticationTypes.ApplicationCookie
                           );
var isPersistent = authenticateResult.Properties.IsPersistent; //// true or false
@using System.Threading.Tasks;
public async Task<ActionResult> SomeMethodName(...) { //etc }