public bool Validate(string token, TokenParams tokenParams) { var secretKey = new SymmetricSecurityKey(Encoding.ASCII .GetBytes(tokenParams.Secret)); var tokenHandler = new JwtSecurityTokenHandler(); try { tokenHandler.ValidateToken(token, new TokenValidationParameters { ValidateIssuerSigningKey = true, ValidateIssuer = true, ValidateAudience = true, ValidIssuer = tokenParams.Issuer, ValidAudience = tokenParams.Audience, IssuerSigningKey = secretKey }, out SecurityToken validatedToken); } catch { return(false); } return(true); }
public string Generate(DeviceModel device, TokenParams tokenParams) { var secretKey = new SymmetricSecurityKey(Encoding.ASCII .GetBytes(tokenParams.Secret)); var handler = new JwtSecurityTokenHandler(); var descriptor = new SecurityTokenDescriptor() { Expires = DateTime.UtcNow.AddHours(2), Issuer = tokenParams.Issuer, Audience = tokenParams.Audience, SigningCredentials = new SigningCredentials(secretKey, SecurityAlgorithms.HmacSha256Signature), Subject = new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.DeviceModel, device.Model), new Claim(ClaimTypes.Manufacturer, device.Manufacturer), new Claim(ClaimTypes.WifiHardwareAddress, device.MacAddress), new Claim(ClaimTypes.BluetootHardwareAddress, device.BluetoothName) }) }; var token = handler.CreateToken(descriptor); RegisterIfNotPresent(device).Wait(); return(handler.WriteToken(token)); }