2014-04-15 6 views
16

ओविन कुकी समाप्त होने के समय मैं किसी प्रकार का उलटी गिनती टाइमर बनाना चाहता हूं। मैं एमवीसी 5 के साथ ओविन का उपयोग कर रहा हूं और जो मैं समझता हूं वह स्लाइडिंग एक्सपेरेशन डिफ़ॉल्ट रूप से चालू है। मैं 'सत्र' का उपयोग नहीं करता क्योंकि मुझे एक वेब फार्म में रहने के लिए इस ऐप की आवश्यकता है (मैं सत्र डेटाबेस को तैनात करने की योजना नहीं बना रहा हूं)।कैसे पता चलेगा कि ओविन कुकी कब समाप्त हो जाएगी?

+0

क्या यह संभव नहीं है? – FrankO

उत्तर

29

कुकी सत्यापन सत्यापन चरण के दौरान आपको CookieValidateIdentityContext को पकड़ने की आवश्यकता है। एक बार जब आप इसे प्राप्त कर लें, तो आपको जो भी चाहिए उसे निकालें और उन्हें Claim या किसी अन्य तरीके से रखें।

Asp.NET पहचान 2.0 के साथ MVC 5 के लिए, आप दो चरणों को पूरा करने की जरूरत है:

  1. कस्टम OnValidateIdentity परिभाषित करें, कुकी जानकारी निकालने, और Claim के रूप में रखना।

    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); 
        } 
    } 
    
  2. पहुँच अपने अपने नियंत्रक तरीकों

    // 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; 
    } 
    

मैं इस दृष्टिकोण का उपयोग अपने आवेदन उपयोगकर्ताओं याद दिलाने के लिए सत्र समय से पहले अपने सत्र का विस्तार करने में Claim

इस पोस्ट को क्रेडिट How do I access Microsoft.Owin.Security.xyz OnAuthenticated context AddClaims values?

+0

क्या आपके पास एक कार्यरत प्रोजेक्ट है जहां यह कोड चलता है? समझने के लिए संघर्ष करना कि ar stampValidator = SecurityStampValidator.OnValidateIdentity के साथ क्या करना है। MyUser> (TimeSpan.FromSeconds (100), शून्य); क्योंकि मेरे पास उपयोगकर्ता प्रबंधक वर्ग और मेरी उपयोगकर्ता श्रेणी नहीं है केवल एक डीटीओ – InTheWorldOfCodingApplications

+0

'मैं इस एप्लिकेशन का उपयोग अपने एप्लिकेशन उपयोगकर्ताओं को सत्र समय से पहले अपने सत्र का विस्तार करने के लिए याद दिलाने के लिए करता हूं।' - आप इसे विस्तारित किए बिना समाप्ति समय कैसे प्राप्त करते हैं? शायद संबंधित - क्या आपके पास स्लाइडिंग एक्सपेरिएशन सक्षम है? – mlhDev

संबंधित मुद्दे