【发布时间】:2016-05-17 02:43:35
【问题描述】:
阅读 ASP.NET Identity 的源代码后,我注意到一些让我感到困惑的东西。有几次,我发现他们使用 ConfigureAwait(false) :
/// <summary>
/// Create a ClaimsIdentity from a user
/// </summary>
/// <param name="manager"></param>
/// <param name="user"></param>
/// <param name="authenticationType"></param>
/// <returns></returns>
public virtual async Task<ClaimsIdentity> CreateAsync(
UserManager<TUser, TKey> manager, TUser user, string authenticationType)
{
if (manager == null) {
throw new ArgumentNullException("manager");
}
if (user == null) {
throw new ArgumentNullException("user");
}
var id = new ClaimsIdentity(authenticationType, UserNameClaimType, RoleClaimType);
id.AddClaim(new Claim(UserIdClaimType, ConvertIdToString(user.Id), ClaimValueTypes.String));
id.AddClaim(new Claim(UserNameClaimType, user.UserName, ClaimValueTypes.String));
id.AddClaim(new Claim(IdentityProviderClaimType, DefaultIdentityProviderClaimValue, ClaimValueTypes.String));
if (manager.SupportsUserSecurityStamp) {
id.AddClaim(new Claim(SecurityStampClaimType, await manager.GetSecurityStampAsync(user.Id).ConfigureAwait(false)));
}
if (manager.SupportsUserRole) {
var roles = await manager.GetRolesAsync(user.Id).ConfigureAwait(false);
foreach (var roleName in roles) {
id.AddClaim(new Claim(RoleClaimType, roleName, ClaimValueTypes.String));
}
}
if (manager.SupportsUserClaim) {
id.AddClaims(await manager.GetClaimsAsync(user.Id).ConfigureAwait(false));
}
return id;
}
我理解使用它的必要性,但我想知道为什么在使用 ASP.NET 标识方法之一之后依赖请求上下文对我们来说是安全的 - 我假设它应该是安全的,因为我没有遇到任何来自 Microsoft 的相反指南。
我们真的有任何保证我们会回到正确的环境吗?如果可以,怎么可能?
【问题讨论】:
标签: c# asp.net async-await asp.net-identity