如何获取isPersistent(AuthenticationProperties)

本文关键字:isPersistent AuthenticationProperties 何获取 获取 | 更新日期: 2023-09-27 17:58:33

我使用的是asp.net MVC 5.2和asp.net标识版本2.2.1以及实体框架6.1.3。在某个时刻,在控制器中,我需要知道当前正在使用的AuthenticationProperties的内容,更具体地说,我需要了解isPersistent的值。

如何获取isPersistent(AuthenticationProperties)

您可以通过调用AuthenticateAsync()通过AspNet.Identity访问当前会话的AuthenticationProperties的内容。此处的表单身份验证不相关,因为会话属于Identity

获取整个AuthenticationProperties对象:

@using Microsoft.AspNet.Identity;
@using System.Threading.Tasks;
public async Task<ActionResult> SomeMethodName(...) {
{    
    var authenticateResult = await HttpContext.GetOwinContext()
                       .Authentication.AuthenticateAsync(
                           DefaultAuthenticationTypes.ApplicationCookie
                       );

有一个authenticateResult,提取属性值的语法(在您的例子中是IsPersistent)是:

var isPersistent = authenticateResult.Properties.IsPersistent; //// true or false

这可能会有所帮助:

var isPersistent  = ((System.Web.Security.FormsIdentity) User.Identity).Ticket.IsPersistent;

   FormsIdentity id = (FormsIdentity)HttpContext.Current.User.Identity;
   FormsAuthenticationTicket ticket = id.Ticket;
   var isPersistent = ticket.IsPersistent.ToString();