public static bool TryIssueToken ( string issuer , string audience , string userName , IEnumerable <Claim> claims , out Microsoft .IdentityModel .Tokens .SecurityToken plainToken , out string secretTokenString , Microsoft .IdentityModel .Tokens .SymmetricSecurityKey signingKey , Microsoft .IdentityModel .Tokens .SigningCredentials signingCredentials //, IIdentity identity = null ) { bool r = false; plainToken = null; secretTokenString = null; try { IIdentity user = new JsonWebTokenUser ( userName , "jwt" , true ); var claimsIdentity = new ClaimsIdentity(user, claims); var securityTokenDescriptor = new Microsoft .IdentityModel .Tokens .SecurityTokenDescriptor() { Issuer = issuer, Audience = audience, IssuedAt = DateTime.Now, Subject = claimsIdentity, SigningCredentials = signingCredentials, }; var tokenHandler = new System .IdentityModel .Tokens .Jwt .JwtSecurityTokenHandler(); plainToken = tokenHandler.CreateToken(securityTokenDescriptor); secretTokenString = tokenHandler.WriteToken(plainToken); r = true; } catch (Exception e) { //throw; } return (r); }
public static bool TryIssueToken ( string issuer , string audience , string userName , IEnumerable <Claim> claims , out SecurityToken plainToken , out string secretTokenString , SecurityKey signingKey , SigningCredentials signingCredentials , DateTime?notBefore = null , DateTime?expires = null ) { bool r = false; plainToken = null; secretTokenString = null; try { IIdentity user = new JsonWebTokenUser ( userName , "jwt" , true ); var claimsIdentity = new ClaimsIdentity(user, claims); var securityTokenDescriptor = new SecurityTokenDescriptor() { Issuer = issuer, Audience = audience, IssuedAt = DateTime.Now, Subject = claimsIdentity, SigningCredentials = signingCredentials, }; if (notBefore != null) { securityTokenDescriptor .NotBefore = notBefore; } if (expires != null) { securityTokenDescriptor .Expires = expires; } var tokenHandler = new JwtSecurityTokenHandler(); plainToken = tokenHandler.CreateToken(securityTokenDescriptor); secretTokenString = tokenHandler.WriteToken(plainToken); r = true; } catch// (Exception e) { //throw; } return (r); }