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


C# ClaimsIdentity.FindFirstValue方法代码示例

本文整理汇总了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"),
            };
        }
开发者ID:TkachukOrest,项目名称:Catch-Me-If-You-Can,代码行数:28,代码来源:ExternalLoginData.cs

示例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))
     };
 }
开发者ID:supperslonic,项目名称:SupperSlonicWebSite,代码行数:11,代码来源:OwinHelper.cs

示例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
     };
 }
开发者ID:Willjobs94,项目名称:empleo-dot-net,代码行数:11,代码来源:UserProfileSocialService.cs

示例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)
            };
        }
开发者ID:AlexeyVoroshak,项目名称:ASP.NET-MVC-5-Tasks-List,代码行数:22,代码来源:ExternalLoginData.cs

示例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);
        }
开发者ID:rioka,项目名称:ASP.NET-MVC-5-Durandal,代码行数:14,代码来源:AuthenticationPropertiesConfig.cs

示例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;
        }
开发者ID:supperslonic,项目名称:SupperSlonicWebSite,代码行数:39,代码来源:ExternalLoginModel.cs

示例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"),
                };
            }
开发者ID:jdoan3,项目名称:Travelfy,代码行数:29,代码来源:AccountController.cs

示例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"),
                };
            }
开发者ID:SkyChang,项目名称:AngularJSAuthentication,代码行数:29,代码来源:AccountController.cs

示例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();
        }
开发者ID:NicolayKosmachev,项目名称:phonegaptest,代码行数:52,代码来源:AccountController.cs

示例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)
                };
            }
开发者ID:Buccaneer,项目名称:3d-mobi-05-eva-rest-back-end,代码行数:22,代码来源:AccountController.cs

示例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)
                };
            }
开发者ID:KristianKirov,项目名称:PoshBoutique,代码行数:29,代码来源:AccountController.cs

示例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;
        }
开发者ID:ReaderOfDream,项目名称:GuessThePicture,代码行数:41,代码来源:ExternalLoginModel.cs

示例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"),
                };
            }
开发者ID:flyeven,项目名称:AngJobs,代码行数:37,代码来源:AccountController.cs

示例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));
			}
开发者ID:Jaecen,项目名称:Peregrine,代码行数:24,代码来源:AccountController.cs

示例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")
     };
 }
开发者ID:quangnc0503h,项目名称:ecommerce,代码行数:17,代码来源:AccountController.cs


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