本文整理汇总了C#中System.Security.Claims.ClaimsIdentity.FindFirstValue方法的典型用法代码示例。如果您正苦于以下问题:C# ClaimsIdentity.FindFirstValue方法的具体用法?C# ClaimsIdentity.FindFirstValue怎么用?C# ClaimsIdentity.FindFirstValue使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Security.Claims.ClaimsIdentity
的用法示例。
在下文中一共展示了ClaimsIdentity.FindFirstValue方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: FromIdentity
public static ExternalLoginData FromIdentity(ClaimsIdentity identity)
{
if (identity == null)
{
return null;
}
Claim providerKeyClaim = identity.FindFirst(ClaimTypes.NameIdentifier);
if (providerKeyClaim == null || String.IsNullOrEmpty(providerKeyClaim.Issuer)
|| String.IsNullOrEmpty(providerKeyClaim.Value))
{
return null;
}
if (providerKeyClaim.Issuer == ClaimsIdentity.DefaultIssuer)
{
return null;
}
return new ExternalLoginData
{
LoginProvider = providerKeyClaim.Issuer,
ProviderKey = providerKeyClaim.Value,
UserName = identity.FindFirstValue(ClaimTypes.Name),
ExternalAccessToken = identity.FindFirstValue("ExternalAccessToken"),
};
}
示例2: CreateUser
public static User CreateUser(ClaimsIdentity claimsIdentity)
{
return new User
{
Id = Int32.Parse(claimsIdentity.FindFirstValue(ClaimTypes.NameIdentifier)),
Email = claimsIdentity.FindFirstValue(ClaimTypes.Email),
FullName = claimsIdentity.FindFirstValue(ClaimTypes.GivenName),
IsVerified = Boolean.Parse(claimsIdentity.FindFirstValue(ClaimTypeIsVerified)),
TimeStamp = ClaimsMapper.GetTimeStamp(claimsIdentity.FindFirstValue(ClaimTypes.Version))
};
}
示例3: GetFromFacebook
private UserProfile GetFromFacebook(ClaimsIdentity identity)
{
string accessToken = identity.FindFirstValue("FacebookAccessToken");
var fbClient = new Facebook.FacebookClient(accessToken);
dynamic fbInfo = fbClient.Get("/me?fields=id,name,email,first_name,last_name");
return new UserProfile
{
Email = fbInfo.email,
Name = fbInfo.name
};
}
示例4: FromIdentity
public static ExternalLoginData FromIdentity(ClaimsIdentity identity)
{
var providerKeyClaim = identity?.FindFirst(ClaimTypes.NameIdentifier);
if (IsNullOrEmpty(providerKeyClaim?.Issuer) || IsNullOrEmpty(providerKeyClaim.Value))
{
return null;
}
if (providerKeyClaim.Issuer == ClaimsIdentity.DefaultIssuer)
{
return null;
}
return new ExternalLoginData
{
LoginProvider = providerKeyClaim.Issuer,
ProviderKey = providerKeyClaim.Value,
UserName = identity.FindFirstValue(ClaimTypes.Name),
Email = identity.FindFirstValue(ClaimTypes.Email)
};
}
示例5: CreateProperties
public static AuthenticationProperties CreateProperties(ClaimsIdentity identity)
{
var roleClaimValues = identity.FindAll(ClaimTypes.Role).Select(c => c.Value);
var roles = string.Join(",", roleClaimValues);
IDictionary<string, string> data = new Dictionary<string, string>
{
{ "userName", identity.FindFirstValue(ClaimTypes.Name) },
{ "userRoles", roles }
};
return new AuthenticationProperties(data);
}
示例6: FromIdentity
public static ExternalLoginModel FromIdentity(ClaimsIdentity identity)
{
if (identity == null)
{
return null;
}
Claim idClaim = identity.FindFirst(ClaimTypes.NameIdentifier);
if (!IsOk(idClaim))
return null;
ExternalLoginModel result = new ExternalLoginModel();
result.IsRegistered = (idClaim.Issuer == ClaimsIdentity.DefaultIssuer);
ExternalLoginProvider loginProvider;
if (!Enum.TryParse<ExternalLoginProvider>(idClaim.OriginalIssuer, ignoreCase: true, result: out loginProvider))
return null;
result.Provider = loginProvider;
if (identity.AuthenticationType == DefaultAuthenticationTypes.ExternalCookie)
{
result.ProviderKey = idClaim.Value;
result.Email = identity.FindFirstValue(ClaimTypes.Email);
result.FullName =
loginProvider == ExternalLoginProvider.Facebook ?
identity.FindFirstValue("urn:facebook:name") :
identity.FindFirstValue(ClaimTypes.Name);
}
else
{
result.ProviderKey = identity.FindFirstValue(ClaimTypes.Sid);
result.Email = identity.FindFirstValue(ClaimTypes.Email);
result.FullName = identity.FindFirstValue(ClaimTypes.GivenName);
}
result.AvatarUrl = identity.FindFirstValue(OwinHelper.ClaimTypeAvatarUrl);
return result;
}
示例7: FromIdentity
public static ExternalLoginData FromIdentity(ClaimsIdentity identity)
{
if (identity == null)
{
return null;
}
Claim providerKeyClaim = identity.FindFirst(ClaimTypes.NameIdentifier);
if (providerKeyClaim == null || String.IsNullOrEmpty(providerKeyClaim.Issuer) || String.IsNullOrEmpty(providerKeyClaim.Value))
{
return null;
}
if (providerKeyClaim.Issuer == ClaimsIdentity.DefaultIssuer)
{
return null;
}
var fb = new FacebookClient(identity.FindFirstValue("ExternalAccessToken"));
dynamic myInfo = fb.Get("/me?fields=email");
return new ExternalLoginData
{
LoginProvider = providerKeyClaim.Issuer,
ProviderKey = providerKeyClaim.Value,
UserName = identity.FindFirstValue(ClaimTypes.Name),
EmailAddress = myInfo.email,
ExternalAccessToken = identity.FindFirstValue("ExternalAccessToken"),
};
}
示例8: FromIdentity
public static ExternalLoginData FromIdentity(ClaimsIdentity identity)
{
if (identity == null)
{
return null;
}
Claim providerKeyClaim = identity.FindFirst(ClaimTypes.NameIdentifier);
//判斷 Issuer 是否為 Local Authority ( 這邊的Issuer為 Google, FB )
if (providerKeyClaim == null || String.IsNullOrEmpty(providerKeyClaim.Issuer) || String.IsNullOrEmpty(providerKeyClaim.Value))
{
return null;
}
if (providerKeyClaim.Issuer == ClaimsIdentity.DefaultIssuer)
{
return null;
}
//傳回要準備寫入 UserLogin DB 的資訊
return new ExternalLoginData
{
LoginProvider = providerKeyClaim.Issuer,
ProviderKey = providerKeyClaim.Value,
UserName = identity.FindFirstValue(ClaimTypes.Name),
ExternalAccessToken = identity.FindFirstValue("ExternalAccessToken"),
};
}
示例9: GetExternalLogin
public async Task<IHttpActionResult> GetExternalLogin(string provider, string error = null)
{
if (error != null)
{
return Redirect(Url.Content("~/") + "#error=" + Uri.EscapeDataString(error));
}
if (!User.Identity.IsAuthenticated)
{
return new ChallengeResult(provider, this);
}
ExternalLoginData externalLogin = ExternalLoginData.FromIdentity(User.Identity as ClaimsIdentity);
if (externalLogin == null)
{
return InternalServerError();
}
if (externalLogin.LoginProvider != provider)
{
Authentication.SignOut(DefaultAuthenticationTypes.ExternalCookie);
return new ChallengeResult(provider, this);
}
IdentityUser user = await UserManager.FindAsync(new UserLoginInfo(externalLogin.LoginProvider,
externalLogin.ProviderKey));
bool hasRegistered = user != null;
if (hasRegistered)
{
Authentication.SignOut(DefaultAuthenticationTypes.ExternalCookie);
ClaimsIdentity oAuthIdentity = await UserManager.CreateIdentityAsync(user, OAuthDefaults.AuthenticationType);
ClaimsIdentity cookieIdentity = await UserManager.CreateIdentityAsync(user, CookieAuthenticationDefaults.AuthenticationType);
AuthenticationProperties properties = ApplicationOAuthProvider.CreateProperties(user.UserName);
Authentication.SignIn(properties, oAuthIdentity, cookieIdentity);
}
else
{
IEnumerable<Claim> claims = externalLogin.GetClaims();
ClaimsIdentity identity = new ClaimsIdentity(claims, OAuthDefaults.AuthenticationType);
CreateUserFileFolder(identity.FindFirstValue(ClaimTypes.Name));
Authentication.SignIn(identity);
}
return Ok();
}
示例10: FromIdentity
public static ExternalLoginData FromIdentity(ClaimsIdentity identity) {
if (identity == null) {
return null;
}
Claim providerKeyClaim = identity.FindFirst(ClaimTypes.NameIdentifier);
if (providerKeyClaim == null || String.IsNullOrEmpty(providerKeyClaim.Issuer)
|| String.IsNullOrEmpty(providerKeyClaim.Value)) {
return null;
}
if (providerKeyClaim.Issuer == ClaimsIdentity.DefaultIssuer) {
return null;
}
return new ExternalLoginData {
LoginProvider = providerKeyClaim.Issuer,
ProviderKey = providerKeyClaim.Value,
UserName = identity.FindFirstValue(ClaimTypes.Email) == null ? identity.FindFirstValue(ClaimTypes.NameIdentifier) + "@" + providerKeyClaim.Issuer.ToLower() + ".com" : identity.FindFirstValue(ClaimTypes.Email)
};
}
示例11: FromIdentity
public static ExternalLoginData FromIdentity(ClaimsIdentity identity)
{
if (identity == null)
{
return null;
}
Claim providerKeyClaim = identity.FindFirst(ClaimTypes.NameIdentifier);
if (providerKeyClaim == null || String.IsNullOrEmpty(providerKeyClaim.Issuer)
|| String.IsNullOrEmpty(providerKeyClaim.Value))
{
return null;
}
if (providerKeyClaim.Issuer == ClaimsIdentity.DefaultIssuer)
{
return null;
}
return new ExternalLoginData
{
LoginProvider = providerKeyClaim.Issuer,
ProviderKey = providerKeyClaim.Value,
Email = identity.FindFirstValue(ClaimTypes.Email),
FirstName = identity.FindFirstValue(ClaimTypes.GivenName),
LastName = identity.FindFirstValue(ClaimTypes.Surname)
};
}
示例12: FromIdentity
public static ExternalLoginModel FromIdentity(ClaimsIdentity identity)
{
if (identity == null)
{
return null;
}
var idClaim = identity.FindFirst(ClaimTypes.NameIdentifier) ??
identity.FindFirst(OAuthClaimsParser.ClaimTypeUid);
if (!IsOk(idClaim))
return null;
var result = new ExternalLoginModel();
result.IsRegistered = (idClaim.Issuer == ClaimsIdentity.DefaultIssuer);
ExternalLoginType loginProvider;
if (!Enum.TryParse(idClaim.OriginalIssuer, true, out loginProvider))
return null;
result.Provider = loginProvider;
if (identity.AuthenticationType == DefaultAuthenticationTypes.ExternalBearer)
{
var parser = OAuthClaimsParserFactory.Create(loginProvider, identity);
result.ProviderKey = parser.GetProviderKey();
result.Email = parser.GetEmail();
result.FirstName = parser.GetFirstName();
result.LastName = parser.GetLasttName();
result.AvatarUrl = parser.GetAvatarUrl();
}
else
{
result.ProviderKey = identity.FindFirstValue(ClaimTypes.Sid);
result.Email = identity.FindFirstValue(ClaimTypes.Email);
result.FirstName = identity.FindFirstValue(ClaimTypes.GivenName);
result.LastName = identity.FindFirstValue(ClaimTypes.Surname);
result.AvatarUrl = identity.FindFirstValue(OAuthClaimsParser.ClaimTypeAvatarUrl);
}
return result;
}
示例13: FromIdentity
public static ExternalLoginData FromIdentity(ClaimsIdentity identity)
{
if (identity == null)
{
return null;
}
Claim providerKeyClaim = identity.FindFirst(ClaimTypes.NameIdentifier);
if (providerKeyClaim == null || String.IsNullOrEmpty(providerKeyClaim.Issuer) || String.IsNullOrEmpty(providerKeyClaim.Value))
{
return null;
}
if (providerKeyClaim.Issuer == ClaimsIdentity.DefaultIssuer)
{
return null;
}
string userId = string.Empty;
if (providerKeyClaim.Issuer.Equals(Stripe, StringComparison.InvariantCultureIgnoreCase))
userId = identity.FindFirst("urn:stripe:account:id").Value;
else
userId = providerKeyClaim.Value;
if (String.IsNullOrEmpty(userId))
return null;
return new ExternalLoginData
{
LoginProvider = providerKeyClaim.Issuer,
ProviderKey = userId,
UserName = identity.FindFirstValue(ClaimTypes.Name),
ExternalAccessToken = identity.FindFirstValue("ExternalAccessToken"),
};
}
示例14: FromIdentity
public static ExternalLoginData FromIdentity(ClaimsIdentity identity)
{
if(identity == null)
return null;
var providerKeyClaim = identity.FindFirst(ClaimTypes.NameIdentifier);
if(providerKeyClaim == null
|| String.IsNullOrEmpty(providerKeyClaim.Issuer)
|| String.IsNullOrEmpty(providerKeyClaim.Value))
{
return null;
}
if(providerKeyClaim.Issuer == ClaimsIdentity.DefaultIssuer)
return null;
return new ExternalLoginData(
loginProvider: providerKeyClaim.Issuer,
providerKey: providerKeyClaim.Value,
userName: identity.FindFirstValue(ClaimTypes.Name),
externalAccessToken: identity.FindFirstValue("ExternalAccessToken"),
email: identity.FindFirstValue(ClaimTypes.Email));
}
示例15: FromIdentity
public static ExternalLoginData FromIdentity(ClaimsIdentity identity)
{
if (identity == null)
return null;
var first = identity.FindFirst("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier");
if (first == null || string.IsNullOrEmpty(first.Issuer) || string.IsNullOrEmpty(first.Value))
return null;
if (first.Issuer == "LOCAL AUTHORITY")
return null;
return new ExternalLoginData
{
LoginProvider = first.Issuer,
ProviderKey = first.Value,
UserName = identity.FindFirstValue("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name"),
ExternalAccessToken = identity.FindFirstValue("ExternalAccessToken")
};
}