6

I'm using asp.net MVC 5.2 with asp.net identity version 2.2.1 and Entity Framework 6.1.3. At some point, in a controller, I need to know the contents of AuthenticationProperties currently being used, more specifically I need to know the value of isPersistent.

  • Possible duplicate of [How to read MVC OWIN AuthenticationProperties?](https://stackoverflow.com/questions/24900113/how-to-read-mvc-owin-authenticationproperties) – Alfred Wallace Dec 14 '18 at 05:46

2 Answers2

2

The contents of AuthenticationProperties for the current session are accessible to you via AspNet.Identity by calling AuthenticateAsync(). Form authentication here is not relevant because the session falls under Identity.

To get the entire AuthenticationProperties object:

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

One you have authenticateResult, the syntax for extracting property values ( in your case IsPersistent) is:

var isPersistent = authenticateResult.Properties.IsPersistent; //// true or false
Alfred Wallace
  • 1,741
  • 1
  • 14
  • 32
-1

May be this can help:

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

Or

   FormsIdentity id = (FormsIdentity)HttpContext.Current.User.Identity;
   FormsAuthenticationTicket ticket = id.Ticket;

   var isPersistent = ticket.IsPersistent.ToString();
Neel
  • 11,625
  • 3
  • 43
  • 61
  • 2
    Hi Neel, I've tried that and I get this error: System.InvalidCastException was unhandled by user code HResult=-2147467262 Message=Unable to cast object of type 'System.Security.Claims.ClaimsIdentity' to type 'System.Web.Security.FormsIdentity'. – Rui Rodrigues Jul 25 '16 at 10:41
  • authCookie returns null, I'm using identity, not forms authentication. – Rui Rodrigues Jul 25 '16 at 11:12
  • Hey there are so many sites which shows this code: https://snipt.net/raw/748b51b939b9db3bba66c74c05baebc7/?nice and http://www.martinwilley.com/net/asp/security/forms.html – Neel Jul 26 '16 at 11:05