Answers for "c# .net "claimtypes.name" oauth2 SecurityTokenDescriptor"

C#
0

c# .net "claimtypes.name" oauth2 SecurityTokenDescriptor

var tokenDescriptor = new SecurityTokenDescriptor
            {
                Issuer = "Issuer",
                Audience = "Audience",
                Subject = new ClaimsIdentity(new Claim[]
                    {
                        new Claim(ClaimTypes.Sid, "1"),
                        new Claim(ClaimTypes.Name, "test user"),
                        new Claim(ClaimTypes.Email, "[email protected]"),
                        new Claim(ClaimTypes.Role, "User")
                    }),
                Expires = DateTime.UtcNow.AddMinutes(15),
                SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(Encoding.ASCII.GetBytes("this is my custom Secret key for authnetication")),
                                                                    SecurityAlgorithms.HmacSha256Signature)
            };

            var tokenHandler = new JwtSecurityTokenHandler();
            var outBoundClaimTypeMap = tokenHandler.OutboundClaimTypeMap;
            // outBoundClaimTypeMap.Clear() will display long names for all claims.
            outBoundClaimTypeMap.Add("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/sid", "sid");
            var token = tokenHandler.CreateToken(tokenDescriptor);
            Console.WriteLine(token);
Posted by: Guest on October-26-2021

C# Answers by Framework

Browse Popular Code Answers by Language