【问题标题】:How to read MVC OWIN AuthenticationProperties?如何阅读 MVC OWIN AuthenticationProperties?
【发布时间】:2014-07-23 00:43:35
【问题描述】:

我在用户登录时设置 IsPersistent,如何读回该值?

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

【问题讨论】:

  • @MarioDS,这个问题没有得到关注是有原因的。因为它不是很清楚。但抛开这一点,您应该首先了解该属性的含义。检查此答案stackoverflow.com/a/32052308/5233410 简而言之,它(属性)不会存储以供回读。它只是指示框架创建一个 cookie。所以我的假设是,一旦 cookie 存在,那么该属性可能设置为 true,否则为 false。
  • @Nkosi 我意识到在我设置赏金后不久 - 不幸的是没有回头:)。我真正想知道的是如何读回其他属性(尤其是在AuthenticationProperties的“字典”中设置的那些)。
  • 也许这个链接会对你有所帮助。 stackoverflow.com/questions/31946582/…
  • @nik 你使用哪种身份验证??
  • 您能否详细解释一下您想在视图中读取身份验证道具的位置、其他控制器等??

标签: c# .net asp.net-mvc owin


【解决方案1】:

AspNet.Identity 使您可以访问会话的boolIsPersistent。读取其值最直接的方法是调用AuthenticateAsync()

@using Microsoft.AspNet.Identity;
var authenticateResult = await HttpContext.GetOwinContext()
                           .Authentication.AuthenticateAsync(
                               DefaultAuthenticationTypes.ApplicationCookie
                           );
var isPersistent = authenticateResult.Properties.IsPersistent; //// true or false

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

@using System.Threading.Tasks;
public async Task<ActionResult> SomeMethodName(...) { //etc }

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

      你可以试试这个,我没有测试过

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

      【讨论】:

      • ...然后...?
      【解决方案4】:

      正如@Nkosi 所说,您可以从 OWIN 检索信息,因为它没有存储在那里,您需要检索 cookie 本身并手动解析信息,但为此您需要一个像this one 这样的 OWIN 中间件,因此您可以根据需要操作 cookie。

      【讨论】:

      • 我认为这种说法是错误的。 IsPersistent实际上存在于HttpContext.GetOwinContext中。
      • 能否请您指出在 HttpContext.GetOwinContext 中可以找到 isPersistent 的确切位置?
      【解决方案5】:

      如果您在 ConfigureAuth 中配置了 CookieAuthOptions,则可以声明 static property CookieAuthOptions,或者任何其他 AuthOption 也应该实现 ISecureDataFormat&lt;AuthenticationTicket&gt;(例如:OAuthBearerOptions.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:你没有提到你需要这个,我认为它会在控制器中。

      【讨论】:

        【解决方案6】:
        var authenticationInfo = await HttpContext.Authentication.GetAuthenticateInfoAsync(DefaultAuthenticationTypes.ApplicationCookie);
        authenticationInfo.Properties ...
        

        我假设 DefaultAuthenticationTypes.ApplicationCookie 包含 cookie 身份验证方案。

        【讨论】:

          【解决方案7】:
          AuthenticateResult authenticateResult = await HttpContext.GetOwinContext().Authentication.AuthenticateAsync(DefaultAuthenticationTypes.ApplicationCookie);
          
          AuthenticationProperties yourAuthenticationProperties = authenticateResult.Properties;
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2020-05-21
            • 1970-01-01
            • 2017-05-12
            • 2023-03-23
            • 2014-03-03
            • 1970-01-01
            • 2011-09-08
            • 2011-01-19
            相关资源
            最近更新 更多