Skip to content

Instantly share code, notes, and snippets.

@zaverden
Created December 7, 2017 09:02
Show Gist options
  • Save zaverden/e6f096f218171d59c0998ab6feb6c684 to your computer and use it in GitHub Desktop.
Save zaverden/e6f096f218171d59c0998ab6feb6c684 to your computer and use it in GitHub Desktop.
ASP.NET Core 2 AddGithub OAuth extension
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Security.Claims;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.OAuth;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json.Linq;
namespace SampleMvc.Extensions
{
public static class AuthenticationBuilderExtensions
{
public static AuthenticationBuilder AddGithub(this AuthenticationBuilder builder, Action<OAuthOptions> configureOptions)
{
builder
.AddOAuth("Github", "GitHub", options =>
{
options.CallbackPath = new PathString("/signin-github");
options.AuthorizationEndpoint = "https://github.com/login/oauth/authorize";
options.TokenEndpoint = "https://github.com/login/oauth/access_token";
options.UserInformationEndpoint = "https://api.github.com/user";
options.ClaimsIssuer = "OAuth2-Github";
options.SaveTokens = true;
// Retrieving user information is unique to each provider.
options.ClaimActions.MapJsonKey(ClaimTypes.NameIdentifier, "id");
options.ClaimActions.MapJsonKey(ClaimTypes.Name, "login");
options.ClaimActions.MapJsonKey("urn:github:name", "name");
options.ClaimActions.MapJsonKey(ClaimTypes.Email, "email", ClaimValueTypes.Email);
options.ClaimActions.MapJsonKey("urn:github:url", "url");
options.Events = new OAuthEvents
{
OnCreatingTicket = async context =>
{
// Get the GitHub user
var request = new HttpRequestMessage(HttpMethod.Get, context.Options.UserInformationEndpoint);
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", context.AccessToken);
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var response = await context.Backchannel.SendAsync(request, context.HttpContext.RequestAborted);
response.EnsureSuccessStatusCode();
var user = JObject.Parse(await response.Content.ReadAsStringAsync());
context.RunClaimActions(user);
}
};
configureOptions?.Invoke(options);
});
return builder;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment