recupères les info utilisateur à l'authentification

This commit is contained in:
2016-06-13 14:32:16 +02:00
parent ee9753129e
commit ffc9fed605

View File

@ -8,6 +8,11 @@ using Microsoft.AspNet.Authentication;
using Microsoft.AspNet.Http; using Microsoft.AspNet.Http;
using Microsoft.AspNet.Authentication.Cookies; using Microsoft.AspNet.Authentication.Cookies;
using Microsoft.Extensions.WebEncoders; using Microsoft.Extensions.WebEncoders;
using Microsoft.AspNet.Authentication.OAuth;
using System.Net.Http;
using System.Net.Http.Headers;
using Newtonsoft.Json.Linq;
using System.Security.Claims;
namespace testOauthClient namespace testOauthClient
{ {
@ -28,7 +33,8 @@ namespace testOauthClient
public void ConfigureServices(IServiceCollection services) public void ConfigureServices(IServiceCollection services)
{ {
services.Configure<SharedAuthenticationOptions>(options => { services.Configure<SharedAuthenticationOptions>(options =>
{
options.SignInScheme = "Bearer"; options.SignInScheme = "Bearer";
}); });
@ -54,12 +60,14 @@ namespace testOauthClient
{ {
app.UseExceptionHandler("/Home/Error"); app.UseExceptionHandler("/Home/Error");
} }
app.UseIISPlatformHandler(options => { app.UseIISPlatformHandler(options =>
{
options.AuthenticationDescriptions.Clear(); options.AuthenticationDescriptions.Clear();
}); });
app.UseStaticFiles(); app.UseStaticFiles();
app.UseCookieAuthentication(new CookieAuthenticationOptions { app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AutomaticAuthenticate = true, AutomaticAuthenticate = true,
AutomaticChallenge = true, AutomaticChallenge = true,
AuthenticationScheme = "Bearer", AuthenticationScheme = "Bearer",
@ -70,7 +78,8 @@ namespace testOauthClient
}); });
app.UseOAuthAuthentication( app.UseOAuthAuthentication(
options => {  options =>
{
options.AuthenticationScheme = "Yavsc"; options.AuthenticationScheme = "Yavsc";
options.AuthorizationEndpoint = "http://dev.pschneider.fr/authorize"; options.AuthorizationEndpoint = "http://dev.pschneider.fr/authorize";
options.TokenEndpoint = "http://dev.pschneider.fr/token"; options.TokenEndpoint = "http://dev.pschneider.fr/token";
@ -78,8 +87,35 @@ namespace testOauthClient
options.ClientId = "21d8bd1b-4aed-4fcb-9ed9-00b43f6a8169"; options.ClientId = "21d8bd1b-4aed-4fcb-9ed9-00b43f6a8169";
options.ClientSecret = "blih"; options.ClientSecret = "blih";
options.Scope.Add("profile"); options.Scope.Add("profile");
// options.SaveTokensAsClaims = true; options.SaveTokensAsClaims = true;
options.UserInformationEndpoint = "http://dev.pschneider.fr/api/me"; options.UserInformationEndpoint = "http://dev.pschneider.fr/api/me";
options.Events = new OAuthEvents
{
OnCreatingTicket = async context =>
{
var request = new HttpRequestMessage(HttpMethod.Get, options.UserInformationEndpoint);
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", context.AccessToken);
var response = await context.Backchannel.SendAsync(request);
response.EnsureSuccessStatusCode();
var payload = JObject.Parse(await response.Content.ReadAsStringAsync());
var identifier = payload.Value<string>("id");
var givenName = payload.Value<string>("givenName");
var emails = payload.Value<JArray>("emails");
string email = null;
if (emails !=null)
email = emails.First?.Value<string>();
var url = payload.Value<string>("url");
context.Identity.AddClaim(
new Claim( ClaimTypes.NameIdentifier,identifier));
context.Identity.AddClaim(
new Claim( ClaimTypes.Name,givenName));
context.Identity.AddClaim(
new Claim( ClaimTypes.Email,email));
}
};
} }
); );