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


C# ClaimsIdentity.RemoveClaim方法代码示例

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


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

示例1: CreateJwtToken

        public static string CreateJwtToken(AuthenticationTicket data, string issuer, SigningCredentials signingCredentials)
        {
            string audience = issuer;

            // As JWT doesn't have a mechanism of passing metadata about what claim should be the name/subject the JWT handler
            // users the default Name claim type. If the identity has another claim type as the name type we need to 
            // switch it to the DefaultNameClaimType.
            var identity = new ClaimsIdentity(data.Identity);
            if (identity.NameClaimType != ClaimsIdentity.DefaultNameClaimType && !string.IsNullOrWhiteSpace(identity.Name))
            {
                identity.AddClaim(new Claim(ClaimsIdentity.DefaultNameClaimType, identity.Name));
                identity.RemoveClaim(identity.Claims.First(c => c.Type == identity.NameClaimType));
            }

            // And now do the same for roles.
            List<Claim> roleClaims = identity.Claims.Where(c => c.Type == identity.RoleClaimType).ToList();
            if (identity.RoleClaimType != ClaimsIdentity.DefaultRoleClaimType && roleClaims.Any())
            {
                foreach (var roleClaim in roleClaims)
                {
                    identity.RemoveClaim(roleClaim);
                    identity.AddClaim(new Claim(ClaimsIdentity.DefaultRoleClaimType, roleClaim.Value, roleClaim.ValueType, roleClaim.Issuer, roleClaim.OriginalIssuer));
                }
            }

            identity.AddClaims(new[]
            {
                new Claim("iat", GetEpocTimeStamp()),
                new Claim("jti", Guid.NewGuid().ToString("N"))
            });

            Lifetime lifetime = new Lifetime(null, null);
            if (data.Properties.IssuedUtc != null || data.Properties.ExpiresUtc != null)
            {
                lifetime = new Lifetime(data.Properties.IssuedUtc != null ? (DateTime?)((DateTimeOffset)data.Properties.IssuedUtc).UtcDateTime : null, data.Properties.ExpiresUtc != null ? (DateTime?)((DateTimeOffset)data.Properties.ExpiresUtc).UtcDateTime : null);
            }

            var handler = new JwtSecurityTokenHandler();
            return handler.CreateToken(issuer, audience, identity, lifetime.Created, lifetime.Expires, signingCredentials).RawData;
        }
开发者ID:Xamarui,项目名称:Katana,代码行数:40,代码来源:SecurityUtils.cs

示例2: GrantRefreshToken

        public override Task GrantRefreshToken(OAuthGrantRefreshTokenContext context)
        {
            var originalClient = context.Ticket.Properties.Dictionary["as:client_id"];
            var currentClient = context.ClientId;

            if (originalClient != currentClient)
            {
                context.SetError("invalid_clientId", "Refresh token is issued to a different clientId.");
                return Task.FromResult<object>(null);
            }

            // Change auth ticket for refresh token requests
            var newIdentity = new ClaimsIdentity(context.Ticket.Identity);
            
            var newClaim = newIdentity.Claims.FirstOrDefault(c => c.Type == "newClaim");
            if (newClaim != null)
            {
                newIdentity.RemoveClaim(newClaim);
            }
            newIdentity.AddClaim(new Claim("newClaim", "newValue"));

            var newTicket = new AuthenticationTicket(newIdentity, context.Ticket.Properties);
            context.Validated(newTicket);

            return Task.FromResult<object>(null);
        }
开发者ID:mdarlea,项目名称:Demo-Restaurant,代码行数:26,代码来源:ApplicationOAuthProvider.cs

示例3: GrantRefreshToken

        public override async Task GrantRefreshToken(OAuthGrantRefreshTokenContext context)
        {
            var originalClient = context.Ticket.Properties.Dictionary["as:client_id"];
            var currentClient = context.OwinContext.Get<string>("as:client_id");

            // enforce client binding of refresh token
            if (originalClient != currentClient)
            {
                context.Rejected();
                return;
            }

            // chance to change authentication ticket for refresh token requests
            var newId = new ClaimsIdentity(context.Ticket.Identity);

            newId.RemoveClaim(newId.FindFirst(c => c.Type == "RefreshToken"));
            newId.AddClaim(new Claim("RefreshToken", Guid.NewGuid().ToString()));

            var newTicket = new AuthenticationTicket(newId, context.Ticket.Properties);
            context.Validated(newTicket);
        }
开发者ID:hoangvv1409,项目名称:codebase,代码行数:21,代码来源:AuthorizationServerProvider.cs

示例4: GrantRefreshToken

        public override Task GrantRefreshToken(OAuthGrantRefreshTokenContext context)
        {
            //We are reading the client id value from the original ticket, 
            //this is the client id which get stored in the magical signed string, 
            //then we compare this client id against the client id sent with the request, 
            //if they are different we’ll reject this request because we need to make 
            //sure that the refresh token used here is bound to the same client when it was generated.

            var originalClient = context.Ticket.Properties.Dictionary["as:client_id"];
            var currentClient = context.ClientId;

            if (originalClient != currentClient)
            {
                context.SetError("invalid_clientId", "Refresh token is issued to a different clientId.");
                return Task.FromResult<object>(null);
            }

            //We have the chance now to add new claims or remove existing claims, 
            //this was not achievable without refresh tokens, then we call 
            //“context.Validated(newTicket)” which will generate new access 
            //token and return it in the response body.


            // Change auth ticket for refresh token requests
            var newIdentity = new ClaimsIdentity(context.Ticket.Identity);

            var newClaim = newIdentity.Claims.Where(c => c.Type == "newClaim").FirstOrDefault();
            if (newClaim != null)
            {
                newIdentity.RemoveClaim(newClaim);
            }
            newIdentity.AddClaim(new Claim("newClaim", "newValue"));

            var newTicket = new AuthenticationTicket(newIdentity, context.Ticket.Properties);
            context.Validated(newTicket);

            return Task.FromResult<object>(null);

            //Lastly after this method executes successfully, the flow for the code 
            //will hit method “CreateAsync” in class “SimpleRefreshTokenProvider” 
            //and a new refresh token is generated and returned in the response 
            //along with the new access token.
        }
开发者ID:Rahul-P,项目名称:WebAPI2-angularSPA,代码行数:43,代码来源:SimpleAuthorizationServerProvider.cs

示例5: CleanUpAllClaimsButEmail

 private static void CleanUpAllClaimsButEmail(ClaimsIdentity identity)
 {
     foreach (var c in identity.Claims.Where(c => c.Type != ClaimTypes.Email).ToArray())
     {
         identity.RemoveClaim(c);
     }
 }
开发者ID:akornatskyy,项目名称:oauth2-social-net,代码行数:7,代码来源:OAuth2Config.cs

示例6: RemoveExternalClaims

        private async Task<ClaimsIdentity> RemoveExternalClaims(ClaimsIdentity userIdentity, string loginProvider) {
            var currentClaims = await UserManager.GetClaimsAsync(userIdentity.GetUserId());
            foreach (var providerClaim in AuthenticationManager.User.Claims.Where(c => c.Type.StartsWith("urn:" + loginProvider.ToLower()))) {
                if (!providerClaim.Type.StartsWith("http://schemas.xmlsoap.org/ws/2005/05/identity/claims")) {
                    if (userIdentity.HasClaim(c => c.Type == providerClaim.Type)) {

                        if (userIdentity.HasClaim(c => c.Type == providerClaim.Type)) {
                            var toRemoveClaim = userIdentity.Claims.FirstOrDefault(c => c.Type == providerClaim.Type);
                            if (toRemoveClaim != null) {
                                userIdentity.RemoveClaim(toRemoveClaim);
                            }
                        }

                        // remove from database
                        var currentClaim = currentClaims.FirstOrDefault(c => c.Type == providerClaim.Type);
                        if (currentClaim != null)
                            await UserManager.RemoveClaimAsync(userIdentity.GetUserId(), currentClaim);
                    }
                }
            }
            return userIdentity;
        }
开发者ID:nonintanon,项目名称:zupzip9,代码行数:22,代码来源:AccountController.cs

示例7: StoreExternalClaims

        private async Task<ClaimsIdentity> StoreExternalClaims(ClaimsIdentity userIdentity) {
            ClaimsIdentity externalIdentity = await AuthenticationManager.GetExternalIdentityAsync(DefaultAuthenticationTypes.ExternalCookie);
            if (externalIdentity != null) {
                var currentClaims = await UserManager.GetClaimsAsync(userIdentity.GetUserId());
                foreach (var claim in externalIdentity.Claims) {
                    if (!claim.Type.StartsWith("http://schemas.xmlsoap.org/ws/2005/05/identity/claims")) {

                        if (userIdentity.HasClaim(c => c.Type == claim.Type && c.Issuer.Contains("LOCAL"))) {
                            var toRemoveClaim = userIdentity.Claims.FirstOrDefault(c => c.Type == claim.Type && c.Issuer.Contains("LOCAL"));
                            if (toRemoveClaim != null) {
                                userIdentity.RemoveClaim(toRemoveClaim);
                            }
                        }

                        if (!userIdentity.HasClaim(claim.Type, claim.Value)) {
                            // Add to claims
                            userIdentity.AddClaim(claim);

                            // Remove current claim
                            var currentClaim = currentClaims.FirstOrDefault(c => c.Type == claim.Type);
                            if (currentClaim != null)
                                await UserManager.RemoveClaimAsync(userIdentity.GetUserId(), currentClaim);

                            // Store claim to database
                            await UserManager.AddClaimAsync(userIdentity.GetUserId(), claim);
                        }
                    }
                }
            }

            return userIdentity;
        }
开发者ID:nonintanon,项目名称:zupzip9,代码行数:32,代码来源:AccountController.cs

示例8: GrantRefreshToken

 public override Task GrantRefreshToken(OAuthGrantRefreshTokenContext context)
 {
     if (context.Ticket.Properties.Dictionary["as:client_id"] != context.ClientId)
     {
         context.SetError("invalid_clientId", "Refresh token is issued to a different clientId.");
         return (Task)Task.FromResult<object>((object)null);
     }
     ClaimsIdentity identity = new ClaimsIdentity((IIdentity)context.Ticket.Identity);
     Claim claim = Enumerable.FirstOrDefault<Claim>(Enumerable.Where<Claim>(identity.Claims, (Func<Claim, bool>)(c => c.Type == "newClaim")));
     if (claim != null)
         identity.RemoveClaim(claim);
     identity.AddClaim(new Claim("newClaim", "newValue"));
     AuthenticationTicket ticket = new AuthenticationTicket(identity, context.Ticket.Properties);
     context.Validated(ticket);
     return (Task)Task.FromResult<object>((object)null);
 }
开发者ID:quangnc0503h,项目名称:ecommerce,代码行数:16,代码来源:SimpleAuthorizationServerProvider.cs

示例9: ChengeItem

 private static void ChengeItem(ClaimsIdentity claimsIdentity, Claim toChange)
 {
     var newClaim = RewriteAndCreate(toChange);
     claimsIdentity.RemoveClaim(toChange);
     claimsIdentity.AddClaim(newClaim);
 }
开发者ID:JonasSyrstad,项目名称:Stardust,代码行数:6,代码来源:DefaultClaimsAppender.cs


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