How to read MVC OWIN AuthenticationProperties?

我们两清 提交于 2019-12-04 22:33:42

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

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

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 }

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;

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.

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.

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

AuthenticationProperties yourAuthenticationProperties = authenticateResult.Properties;

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

I asume DefaultAuthenticationTypes.ApplicationCookie contains cookie authentication scheme.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!