Chciałbym stworzyć pewnego rodzaju zegar odliczający czas od wygaśnięcia ciasteczka OWIN. Używam OWIN z MVC 5 iz tego co rozumiem domyślnie SlidingExpiration jest włączone. Nie używam "sesji", ponieważ potrzebuję tej aplikacji do życia w farmie sieciowej (nie planuję wdrażania bazy danych sesji).Jak sprawdzić, kiedy ciasteczko OWIN wygaśnie?
Odpowiedz
Wszystko, czego potrzebujesz, to zdobycie numeru CookieValidateIdentityContext
podczas etapu sprawdzania ciasteczek. Gdy już to zrobisz, wyodrębnij wszystko, czego potrzebujesz, i zachowaj je tak, jak lubisz.
Dla MVC 5 z ASP.NET Identity 2.0, trzeba wykonać dwie czynności:
Definiowanie niestandardowych
OnValidateIdentity
, wydobyć informacje z plików cookie i zachować go jakoClaim
.public class Startup { public void Configuration(IAppBuilder app) { app.UseCookieAuthentication(new CookieAuthenticationOptions { AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie, Provider = new CookieAuthenticationProvider { OnValidateIdentity = MyCustomValidateIdentity //refer to the implementation below } } } // this method will be called on every request // it is also one of the few places where you can access unencrypted cookie content as CookieValidateIdentityContext // once you get cookie information you need, keep it as one of the Claims // please ignore the MyUserManager and MyUser classes, they are only for sample, you should have yours private static Task MyCustomValidateIdentity(CookieValidateIdentityContext context) { // validate security stamp for 'sign out everywhere' // here I want to verify the security stamp in every 100 seconds. // but I choose not to regenerate the identity cookie, so I passed in NULL var stampValidator = SecurityStampValidator.OnValidateIdentity<MyUserManager<Myuser>. MyUser>(TimeSpan.FromSeconds(100), null); stampValidator.Invoke(context); // here we get the cookie expiry time var expireUtc = context.Properties.ExpiresUtc; // add the expiry time back to cookie as one of the claims, called 'myExpireUtc' // to ensure that the claim has latest value, we must keep only one claim // otherwise we will be having multiple claims with same type but different values var claimType = "myExpireUtc"; var identity = context.Identity; if(identity.HasClaim(c=> c.Type == claimType)) { var existingClaim = identity.FindFirst(claimType); identity.RemoveClaim(existingClaim); } var newClaim = new Claim(claimType, expireUtc.Value.UtcTicks.ToString()); context.Identity.AddClaim(newClaim); return Task.FromResult(0); } }
dostęp do swoich
Claim
w twoich metod kontrolera// since expiry time has now become part of your claims, you now can get it back easily // this example just returns the remaining time in total seconds, as a string value // assuming this method is part of your controller methods public string RemainingTime() { var identity = User.Identity as ClaimsIdentity; var claimType = "myExpireUtc"; //NOTE: must be the same key value "myExpireUtc" defined in code shown above if(identity != null && identity.HasClaim(c=> c.Type == claimType)) { var expireOn = identity.FindFirstValue(claimType); DateTimeOffset currentUtc = DateTimeOffset.UtcNow; DateTimeOffset? expireUtc = new DateTimeOffset(long.Parse(expireOn), TimeSpan.Zero); var remaining = (expireUtc.Value - currentUtc).TotalSeconds; return remaining.ToString(); } return string.Empty; }
używam tego podejścia przypomnieć moje użytkownikom aplikacji rozszerzyć swoją sesję przed limit czasu sesji.
Kredyt do tego stanowiska How do I access Microsoft.Owin.Security.xyz OnAuthenticated context AddClaims values?
Czy masz działający projekt, w którym działa ten kod? Stara się zrozumieć, co zrobić z ar pieczęćValidator = SecurityStampValidator.OnValidateIdentity
"Używam tego podejścia, aby przypomnieć użytkownikom mojej aplikacji, aby przedłużyli swoją sesję przed upływem limitu czasu sesji." - w jaki sposób zdobywasz czas wygaśnięcia bez przedłużania go? Być może powiązane - czy masz włączoną funkcję ślizgania przesuwnego? – mlhDev
Czy to nie jest możliwe? – FrankO