System.Security.Claims.ClaimsIdentity.AddClaim C# (CSharp) Метод

AddClaim() публичный Метод

Adds a single Claim to an internal list.
If Claim.Subject != this, then Claim.Clone(this) is called before the claim is added.
if 'claim' is null.
public AddClaim ( Claim claim ) : void
claim Claim the add.
Результат void
        public virtual void AddClaim(Claim claim)
        {
            if (claim == null)
            {
                throw new ArgumentNullException(nameof(claim));
            }

            Contract.EndContractBlock();

            if (object.ReferenceEquals(claim.Subject, this))
            {
                _instanceClaims.Add(claim);
            }
            else
            {
                _instanceClaims.Add(claim.Clone(this));
            }
        }

Same methods

ClaimsIdentity::AddClaim ( System claim ) : void

Usage Example

        public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
        {
            //Cors
            context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });

            //Validate the User
            using (var authRepository = new AuthorizationRepository())
            {
                var user = await authRepository.FindUser(context.UserName, context.Password);

                if (user == null)
                {
                    context.SetError("invalid_grant", "The username or password is incorrect");

                    return;
                }
                else
                {
                    var token = new ClaimsIdentity(context.Options.AuthenticationType);

                    token.AddClaim(new Claim("sub", context.UserName));
                    token.AddClaim(new Claim("role", "user"));

                    context.Validated(token);
                }
            }
        }
All Usage Examples Of System.Security.Claims.ClaimsIdentity::AddClaim