PDA

View Full Version : سوال: توضیحی در مورد Provider کلاس CookieAuthenticationOptions



mahmud_rastin
چهارشنبه 04 شهریور 1394, 11:07 صبح
سلام به کاربران عزیز

تو کلاس Startup مربوط به یک پروژه MVC اومده این تکه کد رو نوشته :


app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Cpanel/Login"),
ExpireTimeSpan = TimeSpan.FromDays(14),
Provider = new CookieAuthenticationProvider
{
OnValidateIdentity =
ThalassemiaObjectFactory.Container.GetInstance<IApplicationUserManager>().OnValidateIdentity()
}
});


3 خط اولش مشخصه. من فقط مشکلم با Provider هستش. میخوام بدونم این چیه و کارش دقیقا چیه. منظورم متد OnValidateIdentity هستش. این اعتبارسنچی دقیقا چیکار میکنه.

کد متد OnValidateIdentity :


public Func<CookieValidateIdentityContext, Task> OnValidateIdentity()
{
return CustomSecurityStampValidator.OnValidateIdentity(
validateInterval: TimeSpan.FromMinutes(0),
regenerateIdentityCallback: GenerateUserIdentityAsync,
getUserIdCallback: identity => identity.GetUserId<string>());
}



کلاس CustomSecurityStampValidator :


public static class CustomSecurityStampValidator
{
/// <summary>
/// Can be used as the ValidateIdentity method for a CookieAuthenticationProvider which will check a user's security
/// stamp after validateInterval
/// Rejects the identity if the stamp changes, and otherwise will call regenerateIdentity to sign in a new
/// ClaimsIdentity
/// </summary>
/// <param name="validateInterval"></param>
/// <param name="regenerateIdentity"></param>
/// <returns></returns>
public static Func<CookieValidateIdentityContext, Task> OnValidateIdentity(
TimeSpan validateInterval, Func<ApplicationUserManager, ApplicationUser, Task<ClaimsIdentity>> regenerateIdentity)
{
return OnValidateIdentity(validateInterval, regenerateIdentity, id => id.GetUserId<string>());
}

/// <summary>
/// Can be used as the ValidateIdentity method for a CookieAuthenticationProvider which will check a user's security
/// stamp after validateInterval
/// Rejects the identity if the stamp changes, and otherwise will call regenerateIdentity to sign in a new
/// ClaimsIdentity
/// </summary>
/// <param name="validateInterval"></param>
/// <param name="regenerateIdentityCallback"></param>
/// <param name="getUserIdCallback"></param>
/// <returns></returns>
public static Func<CookieValidateIdentityContext, Task> OnValidateIdentity(
TimeSpan validateInterval, Func<ApplicationUserManager, ApplicationUser, Task<ClaimsIdentity>> regenerateIdentityCallback,
Func<ClaimsIdentity, string> getUserIdCallback)
{
if (getUserIdCallback == null)
{
throw new ArgumentNullException("getUserIdCallback");
}
return async context =>
{
var currentUtc = DateTimeOffset.UtcNow;
if (context.Options != null && context.Options.SystemClock != null)
{
currentUtc = context.Options.SystemClock.UtcNow;
}
var issuedUtc = context.Properties.IssuedUtc;

// Only validate if enough time has elapsed
var validate = (issuedUtc == null);
if (issuedUtc != null)
{
var timeElapsed = currentUtc.Subtract(issuedUtc.Value);
validate = timeElapsed > validateInterval;
}

if (validate)
{
var manager = context.OwinContext.GetUserManager<ApplicationUserManager>();
var userId = getUserIdCallback(context.Identity);
if (manager != null)
{
var user = await manager.FindByIdAsync(userId).WithCurrentCulture() ;
var reject = true;
// Refresh the identity if the stamp matches, otherwise reject
if (user != null && manager.SupportsUserSecurityStamp)
{
var securityStamp =
context.Identity.FindFirstValue(Constants.DefaultS ecurityStampClaimType);
if (securityStamp == await manager.GetSecurityStampAsync(userId).WithCurrentC ulture())
{
reject = false;
// Regenerate fresh claims if possible and resign in
if (regenerateIdentityCallback != null)
{
var identity = await regenerateIdentityCallback.Invoke(manager, user).WithCurrentCulture();
if (identity != null)
{
// Fix for regression where this value is not updated
// Setting it to null so that it is refreshed by the cookie middleware
context.Properties.IssuedUtc = null;
context.Properties.ExpiresUtc = null;
context.OwinContext.Authentication.SignIn(context. Properties, identity);
}
}
}
}
if (reject)
{
context.RejectIdentity();
context.OwinContext.Authentication.SignOut(context .Options.AuthenticationType);
}
}
}
};
}
}





ممنون میشم توضیح مختصری بدید.

نکته : طوری که کدهاشو خوندم حدس میزنم کارش اینکه که بعد از متد مشخصه ( در اینجا 14 روز ) میاد کاربر رو SignOut میکنه.

ممنونم.