20

I'm setting IsPersistent when signing the user in, how to read that value back?

var identity = await UserManager.CreateIdentityAsync(appUser, DefaultAuthenticationTypes.ApplicationCookie);
HttpContext.GetOwinContext().Authentication.SignIn(new AuthenticationProperties() { IsPersistent = false }, identity);
nik
  • 1,471
  • 1
  • 12
  • 16
  • 2
    @MarioDS, there is a reason this question has not received attention. Being that it is not very clear. But putting that aside you should first understand what the property is about. check this answer http://stackoverflow.com/a/32052308/5233410 In short it (the property) is not stored to be read back. It just instructs the framework to create a cookie. So my assumption is that once the cookie is present then the property was probably set to true, and would be otherwise false. – Nkosi Feb 01 '17 at 09:48
  • @Nkosi I realized that soon after I set the bounty - no turning back unfortunately :). What I actually wanted to know is how to read other properties back (especially the ones set in the "Dictionary" of AuthenticationProperties). – MarioDS Feb 01 '17 at 10:31
  • maybe this link will help you. http://stackoverflow.com/questions/31946582/how-ispersistent-works-in-owin-cookie-authentication – Baqar Gogia Feb 07 '17 at 12:12
  • @nik which authentication u use?? – Dhiren Patel Feb 08 '17 at 08:22
  • Could you explain more details about where you want to read authentication props in view, other controller, etc?? – Code_Worm Sep 08 '17 at 15:31

7 Answers7

1

AspNet.Identity gives you access to the bool value of IsPersistent for the session. The most direct way to read its value is to call AuthenticateAsync():

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

Note that you will need to wrap this in an async method, such as:

@using System.Threading.Tasks;
public async Task<ActionResult> SomeMethodName(...) { //etc }
Alfred Wallace
  • 1,741
  • 1
  • 14
  • 32
0

Since there isn't much description, I am not sure about the context. Anyway, you can get all the AuthenticationProperties that were provided with the sign in call when you perform authentication on the current request. Code would be..

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

AuthenticationProperties yourAuthenticationProperties = authenticateResult.Properties;
Rama Kathare
  • 920
  • 9
  • 29
0

can you please try this, I haven't test it

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

As @Nkosi said you can not retrieve the information from the OWIN, because it's not stored there, you need to retrieve the cookie itself and parse the information manually, but for that you will need a OWIN Middleware like this one so you can manipulate your cookie as you want.

Alexrgs
  • 831
  • 8
  • 20
0

You can declare a static property CookieAuthOptions if you configured CookieAuthOptions in ConfigureAuth or any other AuthOption should also implement ISecureDataFormat<AuthenticationTicket>(eg: OAuthBearerOptions.AccessTokenFormat). This one contains Protect & Unprotect methods.

Whenever you need to access AuthenticationProperties, you have to be able to get a grip of Owin context to get a reference to a ticket that implements your correct format.

As a basic example steps,

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

to get what you want.

PS: You did not mention where you need this and I assumed it would be in a controller.

Minus
  • 729
  • 8
  • 20
0
AuthenticateResult authenticateResult = await HttpContext.GetOwinContext().Authentication.AuthenticateAsync(DefaultAuthenticationTypes.ApplicationCookie);

AuthenticationProperties yourAuthenticationProperties = authenticateResult.Properties;
Brandon
  • 68,708
  • 30
  • 194
  • 223
Hardi
  • 1
  • 2
0
var authenticationInfo = await HttpContext.Authentication.GetAuthenticateInfoAsync(DefaultAuthenticationTypes.ApplicationCookie);
authenticationInfo.Properties ...

I asume DefaultAuthenticationTypes.ApplicationCookie contains cookie authentication scheme.

Mabakay
  • 328
  • 1
  • 8