When a user logs in to an ASP.NET MVC site using an ASP.NET Identity system, then the second parameter SignInManager.SignIn(user,IsPErsistent,false) indicates whether the user should remember or not. I have such a question, and how can I find out from the site code of the current user in what mode is logged in. I need this because I change the username and I need to re-log it, but I do not understand where to get this variable.
- IsPersistent = true sets expire cookies. false does not. You can try to dig in this direction. - PashaPash ♦
- @PashaPash checked cookies, there is such a cookie named 'ASP.NET_SessionId' and there Expire in both cases stands on {01.01.0001 0:00:00} - Dmitry Polyanin
|
3 answers
using System.Web; using System.Web.Security; public static bool IsFormsAuthCookiePersistent() { if (HttpContext.Current != null && HttpContext.Current.Request != null) { HttpCookie cookie = HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName]; if (cookie != null && !string.IsNullOrEmpty(cookie.Value)) { try { FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(cookie.Value); return ticket.IsPersistent; } catch (Exception) { } } } return false; } - Oh cool! I did not expect to get an answer. I will try in the coming days and accomplish your goal. - Dmitry Polyanin
HttpContext.Current.Request.CookiesI do not haveFormsAuthentication.FormsCookieName, so I did not understand FormsAuthentification. This line in the config is<modules> <remove name="FormsAuthentication" /> </modules>. I do not know how this type of authentication is called, but this is a standard project with a VS template, with the indication of Individual Users Accounts - Dmitry Polyanin
|
I would advise you to look at the availability of "Experies date" from ".AspNet.ApplicationCookie". If date is set to value, "IsPErsistent = true"
- Both here and there ExpireDate costs {01.01.0001 0:00:00}, or I'm looking at something wrong. - Dmitry Polyanin
- mda ... why is that even if the date is set in a cookie, when reading this cookie, the date is still zeros. - Laziz Ergashev
- It is probably better to set another cookie and define the same date as the cookie with "Remember me" (set App_Start / Startup.Auth / app.UseCookieAuthentication (new CookieAuthenticationOptions {ExpireTimeSpan = TimeSpan.FromHours (15)}), and check if this cookie exists, mean "Remember me" - Laziz Ergashev
- Yes, you can, or just write to the database. I wanted to write to the database. - Dmitry Polyanin
- In general, I need it to change the user name, when he changes his name in his profile, he needs to re-log. I already know how to solve it. I'll just log him out, let him log in again. Of course, this is not the most beautiful decision, but the situation is very rare with the change of name, I think this solution is quite suitable for her. - Dmitry Polyanin
|
if(Request.IsAuthenticated) { } else { ] - You can find out from the Request. Both in the controller and in the presentation. - Vivus
- the only question is not about it :) the question of how to check if a constant cookie is set (which survives the browser restart) or temporary (which will die when restarted). in both cases, Request.IsAuthenticated - true - the user is logged in. - PashaPash ♦
- @PashaPash need to see which cookies on the site in the case of IsPersistent and which otherwise, it will turn out to understand what the difference is - Dmitry Polyanin
|