本文整理汇总了C#中System.Security.Claims.ClaimsIdentity.GenerateToken方法的典型用法代码示例。如果您正苦于以下问题:C# ClaimsIdentity.GenerateToken方法的具体用法?C# ClaimsIdentity.GenerateToken怎么用?C# ClaimsIdentity.GenerateToken使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Security.Claims.ClaimsIdentity
的用法示例。
在下文中一共展示了ClaimsIdentity.GenerateToken方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetUserInfo
public async Task<GetUserInfoResponse> GetUserInfo(ClaimsIdentity claimsIdentity)
{
bool isAzureActiveDirectoryUser = false;
var hasIssClaim = claimsIdentity.FindFirst("iss");
if (hasIssClaim != null)
{
if (hasIssClaim.Value.Contains("sts.windows.net"))
{
isAzureActiveDirectoryUser = true;
}
}
var userName = claimsIdentity.GetUserName();
var userInfo = await _databaseRepository.GetUserInfo(userName);
// if the user is unknown, we store it in our own database.
if (userInfo == null && isAzureActiveDirectoryUser)
{
var firstName = claimsIdentity.Claims.Where(x => x.Type == System.IdentityModel.Claims.ClaimTypes.GivenName).Select(x => x.Value).FirstOrDefault();
var lastName = claimsIdentity.Claims.Where(x => x.Type == System.IdentityModel.Claims.ClaimTypes.Surname).Select(x => x.Value).FirstOrDefault();
var user = await _userService.Create(userName, userName, firstName, lastName);
await _databaseRepository.AddToUserList(user.Id, user.UserName, user.FirstName, user.LastName);
userInfo = await _databaseRepository.GetUserInfo(userName);
}
var roleClaims = new List<Claim>();
foreach (var userRole in userInfo.Roles)
{
roleClaims.Add(new Claim(ClaimTypes.Role, userRole));
}
var localClaimsIdentity = new ClaimsIdentity(
new List<Claim>
{
new Claim(ClaimTypes.Name, userInfo.UserName),
new Claim(ClaimTypes.GivenName, userInfo.Profile.FirstName),
new Claim(ClaimTypes.Surname, userInfo.Profile.LastName),
new Claim(ClaimTypes.NameIdentifier, userInfo.Id.ToString())
});
localClaimsIdentity.AddClaims(roleClaims);
var token = localClaimsIdentity.GenerateToken(_configurationService.LocalAuthentication.AudienceId, _configurationService.LocalAuthentication.AudienceSecret, _configurationService.LocalAuthentication.Issuer, DateTime.UtcNow.AddHours(-1), DateTime.UtcNow.AddDays(10));
userInfo.UserToken = token;
return userInfo;
}