Skip to content

Instantly share code, notes, and snippets.

@benfoster
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benfoster/9409094 to your computer and use it in GitHub Desktop.
Save benfoster/9409094 to your computer and use it in GitHub Desktop.
Refreshing claims using Federated Identity Session Authentication Module (SAM)
public async Task RefreshClaimsAsync()
{
SessionSecurityToken token;
if (FederatedAuthentication.SessionAuthenticationModule.TryReadSessionTokenFromCookie(out token))
{
// obtain their API access token using current identity
var accessToken = await tokenManager.GetTokenFromStoreAsync(token.ClaimsPrincipal.Identity.Name);
// Reload the claims
await WriteSessionToken(accessToken);
}
}
private async Task WriteSessionToken(ApiAccessToken accessToken)
{
var client = ApiClient.FromConnectionString(Constants.CMSAPIConnectionStringName)
.Configure(cfg => cfg.UseSessionTokenAuthentication(accessToken.GetToken()).LogUsing(logger));
var authClient = client.GetAuthClient();
var identity = await authClient.GetIdentityAsync();
var claimsPrincipal = new ClaimsPrincipal(new ClaimsIdentity(identity.GetClaims(), "Forms"));
logger.Trace("Writing SAM token to cookie.");
// Start SAM
var sessionToken = new SessionSecurityToken(claimsPrincipal);
FederatedAuthentication.SessionAuthenticationModule.WriteSessionTokenToCookie(sessionToken);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment