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


C# ClaimsPrincipal.GetAppServiceIdentityAsync方法代码示例

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


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

示例1: GetCurrentUserRegistrationReferenceAsync

        /// <summary>
        /// Extracts the User details accessing the service as a unique id in the form
        /// of "{authprovider}:{uniqueId}" using ProviderCrednetials for the logged
        /// in user.
        /// </summary>
        /// <param name="principal">The principal accessing the service.</param>
        /// <param name="request">The HttpRequest used to access the service.</param>
        /// <returns>The unique user id.</returns>
        public async Task<string> GetCurrentUserRegistrationReferenceAsync(ClaimsPrincipal principal, HttpRequestMessage request)
        {
            string provider = principal?.FindFirst("http://schemas.microsoft.com/identity/claims/identityprovider").Value;

            ProviderCredentials creds = null;
            if (string.Equals(provider, "facebook", StringComparison.OrdinalIgnoreCase))
            {
                creds = await principal.GetAppServiceIdentityAsync<FacebookCredentials>(request);
            }
            else if (string.Equals(provider, "google", StringComparison.OrdinalIgnoreCase))
            {
                creds = await principal.GetAppServiceIdentityAsync<GoogleCredentials>(request);
            }
            else if (string.Equals(provider, "twitter", StringComparison.OrdinalIgnoreCase))
            {
                creds = await principal.GetAppServiceIdentityAsync<TwitterCredentials>(request);
            }
            else if (string.Equals(provider, "microsoftaccount", StringComparison.OrdinalIgnoreCase))
            {
                creds = await principal.GetAppServiceIdentityAsync<MicrosoftAccountCredentials>(request);
            }

            if (creds == null)
            {
                throw ServiceExceptions.UserNullException();
            }

            // Format user details in the desired form of {authprovider}:{uniqueId}
            string authProvider = creds.Provider;
            string uniqueId = creds.UserClaims.FirstOrDefault(c => c.Type.Equals(ClaimTypes.NameIdentifier))?.Value;
            var uniqueUserName = $"{authProvider}:{uniqueId}";

            return uniqueUserName;
        }
开发者ID:Microsoft,项目名称:Appsample-Photosharing,代码行数:42,代码来源:DefaultUserRegistrationReferenceProvider.cs

示例2: GetIdentityAsync_Throws_IfNoIssuer

        public async Task GetIdentityAsync_Throws_IfNoIssuer()
        {
            // Arrange
            ClaimsPrincipal user = new ClaimsPrincipal(CreateMockClaimsIdentity(new Claim[] { new Claim("notiss", "nope") }, true));
            HttpRequestMessage request = new HttpRequestMessage();
            request.Headers.Add(AuthHeaderName, "some jwt");

            // Act
            ArgumentOutOfRangeException ex = await Assert.ThrowsAsync<ArgumentOutOfRangeException>(async () => await user.GetAppServiceIdentityAsync<FacebookCredentials>(request));

            // Assert
            Assert.Equal("The IPrincipal's Claims must contain an 'iss' Claim.", ex.Message);
        }
开发者ID:jwallra,项目名称:azure-mobile-apps-net-server-1,代码行数:13,代码来源:ServiceUserTests.cs

示例3: GetIdentityAsync_ReturnsNull_IfUserNotAuthenticated

        public async Task GetIdentityAsync_ReturnsNull_IfUserNotAuthenticated()
        {
            // Arrange
            ClaimsPrincipal user = new ClaimsPrincipal(CreateMockClaimsIdentity(Enumerable.Empty<Claim>(), false));
            HttpRequestMessage request = new HttpRequestMessage();

            // Act
            var tokenResult = await user.GetAppServiceIdentityAsync<FacebookCredentials>(request);

            // Assert
            Assert.Null(tokenResult);
        }
开发者ID:jwallra,项目名称:azure-mobile-apps-net-server-1,代码行数:12,代码来源:ServiceUserTests.cs

示例4: GetIdentityAsync_ReturnsNull_IfMobileAppAuthenticationTokenIsNullOrEmpty

        public async Task GetIdentityAsync_ReturnsNull_IfMobileAppAuthenticationTokenIsNullOrEmpty(string token)
        {
            // Arrange
            ClaimsPrincipal user = new ClaimsPrincipal(CreateMockClaimsIdentity(Enumerable.Empty<Claim>(), true));
            HttpRequestMessage request = new HttpRequestMessage();
            request.RequestUri = new Uri(TestLocalhostUrl);
            request.Headers.Add(AuthHeaderName, token);

            // Act
            var tokenResult = await user.GetAppServiceIdentityAsync<FacebookCredentials>(request);

            // Assert
            Assert.Null(tokenResult);
        }
开发者ID:jwallra,项目名称:azure-mobile-apps-net-server-1,代码行数:14,代码来源:ServiceUserTests.cs

示例5: GetIdentityAsync_Succeeds

        public async Task GetIdentityAsync_Succeeds()
        {
            // Arrange
            TokenEntry tokenEntry = new TokenEntry("facebook");
            tokenEntry.UserId = "userId";
            tokenEntry.AccessToken = "accessToken";
            tokenEntry.UserClaims = new List<ClaimSlim>() { new ClaimSlim(ClaimTypes.NameIdentifier, "11111111") };

            HttpResponseMessage response = CreateOkResponseWithContent(tokenEntry);
            MockHttpMessageHandler handler = new MockHttpMessageHandler(response);

            ClaimsPrincipal user = new ClaimsPrincipal(CreateMockClaimsIdentity(new[] { new Claim(JwtRegisteredClaimNames.Iss, "http://contoso.com") }, true));
            HttpRequestMessage request = new HttpRequestMessage();
            request.Headers.Add(AuthHeaderName, "token");

            //Act
            FacebookCredentials creds = await user.GetAppServiceIdentityAsync<FacebookCredentials>(request, new HttpClient(handler));

            // Assert
            Assert.Equal(tokenEntry.UserId, creds.UserId);
            Assert.Equal(tokenEntry.AccessToken, creds.AccessToken);
            Assert.Equal("Facebook", creds.Provider);
            Assert.Equal(1, tokenEntry.UserClaims.Count());
            ClaimSlim claim = tokenEntry.UserClaims[0];
            Assert.Equal(ClaimTypes.NameIdentifier, claim.Type);
            Assert.Equal("11111111", claim.Value);

            Assert.Equal("http://contoso.com/.auth/me?provider=facebook", handler.ActualRequest.RequestUri.ToString());
            Assert.Equal("token", handler.ActualRequest.GetHeaderOrDefault(AuthHeaderName));
        }
开发者ID:jwallra,项目名称:azure-mobile-apps-net-server-1,代码行数:30,代码来源:ServiceUserTests.cs


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