Skip to content

Instantly share code, notes, and snippets.

@ryanvgates
Last active September 24, 2015 15:43
Show Gist options
  • Save ryanvgates/56236be70249339c64c5 to your computer and use it in GitHub Desktop.
Save ryanvgates/56236be70249339c64c5 to your computer and use it in GitHub Desktop.
IdentityServer3 Prototype IdServer Startup
[HttpPost]
[ActionName("Index")]
public async Task<ActionResult> GetToken()
{
var client = new TokenClient(
"https://localhost:44333/connect/token",
"AuthCode",
"secret");
var code = Request.QueryString["code"];
var response = await client.RequestAuthorizationCodeAsync(code, "https://localhost:44300/callback");
await ValidateResponseAndSignInAsync(response, "test");
return View("Token", response);
}
private async Task ValidateResponseAndSignInAsync(TokenResponse response, string nonce)
{
if (!string.IsNullOrWhiteSpace(response.IdentityToken))
{
var tokenClaims = ValidateToken(response.IdentityToken, nonce);
var claims = new List<Claim>();
if (!string.IsNullOrWhiteSpace(response.AccessToken))
{
claims.AddRange(await GetUserInfoClaimsAsync(response.AccessToken));
claims.Add(new Claim("access_token", response.AccessToken));
claims.Add(new Claim("expires_at", (DateTime.UtcNow.ToEpochTime() + response.ExpiresIn).ToDateTimeFromEpoch().ToString()));
}
if (!string.IsNullOrWhiteSpace(response.RefreshToken))
{
claims.Add(new Claim("refresh_token", response.RefreshToken));
}
var id = new ClaimsIdentity(claims, "Cookies");
Request.GetOwinContext().Authentication.SignIn(id);
}
}
using IdentityServer3.Core.Configuration;
using Owin;
namespace IdentityServerAuthCodeFun
{
internal class Startup
{
public void Configuration(IAppBuilder appBuilder)
{
var options = new IdentityServerOptions()
{
Factory = new IdentityServerServiceFactory()
.UseInMemoryClients(Clients.Get())
.UseInMemoryScopes(Scopes.Get())
.UseInMemoryUsers(Users.Get())
};
appBuilder.UseIdentityServer(options);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment