当前位置: 首页>>代码示例>>C#>>正文


C# ClaimsPrincipal.GetObjectIdentifierValue方法代码示例

本文整理汇总了C#中System.Security.Claims.ClaimsPrincipal.GetObjectIdentifierValue方法的典型用法代码示例。如果您正苦于以下问题:C# ClaimsPrincipal.GetObjectIdentifierValue方法的具体用法?C# ClaimsPrincipal.GetObjectIdentifierValue怎么用?C# ClaimsPrincipal.GetObjectIdentifierValue使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Security.Claims.ClaimsPrincipal的用法示例。


在下文中一共展示了ClaimsPrincipal.GetObjectIdentifierValue方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: GetAccessTokenForResourceAsync

        private async Task<string> GetAccessTokenForResourceAsync(string resource, ClaimsPrincipal user)
        {
            var userId = user.GetObjectIdentifierValue();
            var issuerValue = user.GetIssuerValue();
            var userName = user.Identity?.Name;

            try
            {
                _logger.BearerTokenAcquisitionStarted(resource, userName, issuerValue);
                var authContext = await CreateAuthenticationContext(user)
                    .ConfigureAwait(false);
                var result = await authContext.AcquireTokenSilentAsync(
                    resource,
                    await _credentialService.GetCredentialsAsync().ConfigureAwait(false),
                    new UserIdentifier(userId, UserIdentifierType.UniqueId))
                    .ConfigureAwait(false);

                _logger.BearerTokenAcquisitionSucceeded(resource, userName, issuerValue);

                return result.AccessToken;
            }
            catch (AdalException ex)
            {
                _logger.BearerTokenAcquisitionFailed(resource, userName, issuerValue, ex);
                throw new AuthenticationException($"AcquireTokenSilentAsync failed for user: {userId}", ex);
            }
        }
开发者ID:Azure-Samples,项目名称:guidance-identity-management-for-multitenant-apps,代码行数:27,代码来源:SurveysTokenService.cs

示例2: BuildCacheKey

        /// <summary>
        /// Builds the cache key to use for this item in the distributed cache.
        /// </summary>
        /// <param name="claimsPrincipal">A <see cref="System.Security.Claims.ClaimsPrincipal"/> for the signed in user</param>
        /// <returns>Cache key for this item.</returns>
        private static string BuildCacheKey(ClaimsPrincipal claimsPrincipal)
        {
            Guard.ArgumentNotNull(claimsPrincipal, nameof(claimsPrincipal));

            string clientId = claimsPrincipal.FindFirstValue("aud", true);
            return string.Format(
                "UserId:{0}::ClientId:{1}",
                claimsPrincipal.GetObjectIdentifierValue(),
                clientId);
        }
开发者ID:Azure-Samples,项目名称:guidance-identity-management-for-multitenant-apps,代码行数:15,代码来源:DistributedTokenCache.cs

示例3: RequestTokenAsync

        /// <summary>
        /// This method acquires an access token using an authorization code and ADAL. The access token is then cached
        /// in a <see cref="TokenCache"/> to be used later (by calls to GetTokenForWebApiAsync).
        /// </summary>
        /// <param name="claimsPrincipal">A <see cref="ClaimsPrincipal"/> for the signed in user</param>
        /// <param name="authorizationCode">a string authorization code obtained when the user signed in</param>
        /// <param name="redirectUri">The Uri of the application requesting the access token</param>
        /// <param name="resource">The resouce identifier of the target resource</param>
        /// <returns>A <see cref="System.Threading.Tasks.Task{Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationResult}"/>.</returns>
        public async Task<AuthenticationResult> RequestTokenAsync(
            ClaimsPrincipal claimsPrincipal,
            string authorizationCode,
            string redirectUri,
            string resource)
        {
            Guard.ArgumentNotNull(claimsPrincipal, nameof(claimsPrincipal));
            Guard.ArgumentNotNullOrWhiteSpace(authorizationCode, nameof(authorizationCode));
            Guard.ArgumentNotNullOrWhiteSpace(redirectUri, nameof(redirectUri));
            Guard.ArgumentNotNullOrWhiteSpace(resource, nameof(resource));

            try
            {
                var userId = claimsPrincipal.GetObjectIdentifierValue();
                var issuerValue = claimsPrincipal.GetIssuerValue();
                _logger.AuthenticationCodeRedemptionStarted(userId, issuerValue, resource);
                var authenticationContext = await CreateAuthenticationContext(claimsPrincipal)
                    .ConfigureAwait(false);
                var authenticationResult = await authenticationContext.AcquireTokenByAuthorizationCodeAsync(
                    authorizationCode,
                    new Uri(redirectUri),
                    await _credentialService.GetCredentialsAsync().ConfigureAwait(false),
                    resource)
                    .ConfigureAwait(false);

                _logger.AuthenticationCodeRedemptionCompleted(userId, issuerValue, resource);
                return authenticationResult;
            }
            catch (Exception ex)
            {
                _logger.AuthenticationCodeRedemptionFailed(ex);
                throw;
            }
        }
开发者ID:Azure-Samples,项目名称:guidance-identity-management-for-multitenant-apps,代码行数:43,代码来源:SurveysTokenService.cs


注:本文中的System.Security.Claims.ClaimsPrincipal.GetObjectIdentifierValue方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。