How to read MVC OWIN AuthenticationProperties?

2019-04-03 22:25发布

问题:

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);

回答1:

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

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


回答2:

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 }


回答3:

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;


回答4:

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.



回答5:

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.



回答6:

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

AuthenticationProperties yourAuthenticationProperties = authenticateResult.Properties;



回答7:

var authenticationInfo = await HttpContext.Authentication.GetAuthenticateInfoAsync(DefaultAuthenticationTypes.ApplicationCookie);
authenticationInfo.Properties ...

I asume DefaultAuthenticationTypes.ApplicationCookie contains cookie authentication scheme.