This repository was archived by the owner on Dec 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
SSO Example C Sharp
Jessica Castrogiovanni edited this page Dec 21, 2016
·
2 revisions
The following example demonstrates building the JWT token in C# using System.IdentityModel.Tokens.
This example was written for ease of understanding, not security. It is not recommended to use the example code without reviewing it against your development security practices.
public virtual string Encode(string name, string email)
{
var handler = new JwtSecurityTokenHandler();
if (_token == null)
{
var tk421 = BuildToken(name, email);
_token = tk421 as JwtSecurityToken;
}
return handler.WriteToken(_token);
}
//role = "patient" or "clinician"
protected override SecurityToken BuildToken(string name, string email, string role)
{
var claimList = new List<Claim>
{
new Claim(ClaimTypes.Name, name),
new Claim(ClaimTypes.Email, email),
new Claim(ClaimTypes.Role, role)
};
var handler = new JwtSecurityTokenHandler();
var tk421 = handler.CreateToken(
subject: new ClaimsIdentity(claimList),
signingCredentials: new SigningCredentials(
_key,
SecurityAlgorithms.RsaSha256Signature,
SecurityAlgorithms.Sha1Digest
),
signatureProvider: new AsymmetricSignatureProvider(
_key,
SecurityAlgorithms.RsaSha1Signature,
true
),
audience: "snapmd",
issuer: "examplehospital"
);
return tk421;
}