get more claims
This commit is contained in:
parent
43b289886e
commit
3df1c1b14d
7 changed files with 135 additions and 169 deletions
|
|
@ -4,8 +4,10 @@ using System.Diagnostics;
|
|||
using System.IO;
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Text.Json;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using IdentityModel.Client;
|
||||
using Microsoft.AspNetCore.Authentication;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
|
@ -36,38 +38,30 @@ namespace testOauthClient.Controllers
|
|||
return View();
|
||||
}
|
||||
|
||||
public async Task<IActionResult> CallApi()
|
||||
{
|
||||
var accessToken = await HttpContext.GetTokenAsync("access_token");
|
||||
public async Task<IActionResult> CallApi()
|
||||
{
|
||||
var accessToken = await HttpContext.GetTokenAsync("access_token");
|
||||
|
||||
var client = new HttpClient();
|
||||
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
|
||||
var content = await client.GetStringAsync("https://localhost:5001/api/me");
|
||||
var client = new HttpClient();
|
||||
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
|
||||
var content = await client.GetStringAsync("https://localhost:5001/api/account/me");
|
||||
|
||||
ViewBag.Json = content;
|
||||
return View("json");
|
||||
}
|
||||
ViewBag.Json = content;
|
||||
return View("json");
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> GetUserInfo(CancellationToken cancellationToken)
|
||||
{
|
||||
var accessToken = await HttpContext.GetTokenAsync("access_token");
|
||||
|
||||
using (var client = new HttpClient())
|
||||
{
|
||||
var request = new HttpRequestMessage(HttpMethod.Get, "https://localhost:5001/api/me");
|
||||
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
|
||||
|
||||
|
||||
var response = await client.SendAsync(request, cancellationToken);
|
||||
response.EnsureSuccessStatusCode();
|
||||
|
||||
return View("UserInfo", model: await response.Content.ReadAsStringAsync());
|
||||
}
|
||||
|
||||
var accessToken = await HttpContext.GetTokenAsync("access_token");
|
||||
var client = new HttpClient();
|
||||
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
|
||||
client.DefaultRequestHeaders.Add("Accept", "application/json");
|
||||
var content = await client.GetStringAsync("https://localhost:5001/api/account/me");
|
||||
var obj = JsonSerializer.Deserialize<JsonElement>(content);
|
||||
return View("UserInfo", obj.ToString());
|
||||
}
|
||||
|
||||
|
||||
public IActionResult About()
|
||||
{
|
||||
ViewData["Message"] = "Your application description page.";
|
||||
|
|
|
|||
|
|
@ -6,67 +6,68 @@ using Microsoft.Extensions.Hosting;
|
|||
using Microsoft.IdentityModel.Tokens;
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
|
||||
public class Startup
|
||||
public class Startup
|
||||
{
|
||||
public void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
public void ConfigureServices(IServiceCollection services)
|
||||
services.AddControllersWithViews();
|
||||
|
||||
JwtSecurityTokenHandler.DefaultMapInboundClaims = false;
|
||||
|
||||
services.AddAuthentication(options =>
|
||||
{
|
||||
services.AddControllersWithViews();
|
||||
options.DefaultScheme = "Cookies";
|
||||
options.DefaultChallengeScheme = "Yavsc";
|
||||
})
|
||||
.AddCookie("Cookies")
|
||||
.AddOpenIdConnect("Yavsc", options =>
|
||||
{
|
||||
options.Authority = "https://localhost:5001";
|
||||
|
||||
JwtSecurityTokenHandler.DefaultMapInboundClaims = false;
|
||||
options.ClientId = "mvc";
|
||||
options.ClientSecret = "49C1A7E1-0C79-4A89-A3D6-A37998FB86B0";
|
||||
options.ResponseType = "code";
|
||||
options.UsePkce = true;
|
||||
options.Scope.Clear();
|
||||
options.Scope.Add("openid");
|
||||
options.Scope.Add("profile");
|
||||
options.Scope.Add("email");
|
||||
|
||||
services.AddAuthentication(options =>
|
||||
{
|
||||
options.DefaultScheme = "Cookies";
|
||||
options.DefaultChallengeScheme = "Yavsc";
|
||||
})
|
||||
.AddCookie("Cookies")
|
||||
.AddOpenIdConnect("Yavsc", options =>
|
||||
{
|
||||
options.Authority = "https://localhost:5001";
|
||||
|
||||
options.ClientId = "mvc";
|
||||
options.ClientSecret = "49C1A7E1-0C79-4A89-A3D6-A37998FB86B0";
|
||||
options.ResponseType = "code";
|
||||
options.UsePkce = true;
|
||||
options.Scope.Clear();
|
||||
options.Scope.Add("openid");
|
||||
options.Scope.Add("profile");
|
||||
options.Scope.Add("email");
|
||||
|
||||
options.GetClaimsFromUserInfoEndpoint = true;
|
||||
options.GetClaimsFromUserInfoEndpoint = true;
|
||||
options.SaveTokens = true;
|
||||
options.ClaimActions.MapUniqueJsonKey("http://schemas.microsoft.com/ws/2008/06/identity/claims/role", "http://schemas.microsoft.com/ws/2008/06/identity/claims/role");
|
||||
|
||||
options.ClaimActions.MapUniqueJsonKey("role", "role");
|
||||
options.ClaimActions.MapUniqueJsonKey("roles", "role");
|
||||
options.ClaimActions.MapUniqueJsonKey("role", "http://schemas.microsoft.com/ws/2008/06/identity/claims/role");
|
||||
options.ClaimActions.MapUniqueJsonKey("roles", "http://schemas.microsoft.com/ws/2008/06/identity/claims/role");
|
||||
options.TokenValidationParameters = new TokenValidationParameters
|
||||
{
|
||||
NameClaimType = "name",
|
||||
RoleClaimType = "role"
|
||||
RoleClaimType = "http://schemas.microsoft.com/ws/2008/06/identity/claims/role"
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
||||
{
|
||||
if (env.IsDevelopment())
|
||||
{
|
||||
app.UseDeveloperExceptionPage();
|
||||
}
|
||||
else
|
||||
{
|
||||
app.UseExceptionHandler("/Home/Error");
|
||||
}
|
||||
|
||||
app.UseStaticFiles();
|
||||
|
||||
app.UseRouting();
|
||||
app.UseAuthentication();
|
||||
app.UseAuthorization();
|
||||
|
||||
app.UseEndpoints(endpoints =>
|
||||
{
|
||||
endpoints.MapDefaultControllerRoute()
|
||||
.RequireAuthorization();
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
||||
{
|
||||
if (env.IsDevelopment())
|
||||
{
|
||||
app.UseDeveloperExceptionPage();
|
||||
}
|
||||
else
|
||||
{
|
||||
app.UseExceptionHandler("/Home/Error");
|
||||
}
|
||||
|
||||
app.UseStaticFiles();
|
||||
|
||||
app.UseRouting();
|
||||
app.UseAuthentication();
|
||||
app.UseAuthorization();
|
||||
|
||||
app.UseEndpoints(endpoints =>
|
||||
{
|
||||
endpoints.MapDefaultControllerRoute()
|
||||
.RequireAuthorization();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,26 +4,6 @@
|
|||
}
|
||||
@using Microsoft.AspNetCore.Authentication
|
||||
|
||||
<h2>Claims</h2>
|
||||
|
||||
<dl>
|
||||
@foreach (var claim in User.Claims)
|
||||
{
|
||||
<dt>@claim.Type</dt>
|
||||
<dd>@claim.Value</dd>
|
||||
}
|
||||
</dl>
|
||||
|
||||
<h2>Properties</h2>
|
||||
|
||||
<dl>
|
||||
@foreach (var prop in (await Context.AuthenticateAsync()).Properties.Items)
|
||||
{
|
||||
<dt>@prop.Key</dt>
|
||||
<dd>@prop.Value</dd>
|
||||
}
|
||||
</dl>
|
||||
|
||||
<div class="jumbotron">
|
||||
@if (User?.Identity?.IsAuthenticated ?? false) {
|
||||
<h1>Welcome, @User.Identity.Name</h1>
|
||||
|
|
@ -57,25 +37,3 @@
|
|||
<a class="btn btn-lg btn-success" href="/signin">Sign in</a>
|
||||
}
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
<h2>Claims</h2>
|
||||
|
||||
<dl>
|
||||
@foreach (var claim in User.Claims)
|
||||
{
|
||||
<dt>@claim.Type</dt>
|
||||
<dd>@claim.Value</dd>
|
||||
}
|
||||
</dl>
|
||||
|
||||
<h2>Properties</h2>
|
||||
|
||||
<dl>
|
||||
@foreach (var prop in (await Context.AuthenticateAsync()).Properties.Items)
|
||||
{
|
||||
<dt>@prop.Key</dt>
|
||||
<dd>@prop.Value</dd>
|
||||
}
|
||||
</dl>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue