IdentityServer8
This commit is contained in:
parent
18ddbead66
commit
a16188338a
47 changed files with 4077 additions and 529 deletions
|
|
@ -12,13 +12,13 @@ using Microsoft.AspNetCore.Mvc;
|
|||
// But, this redirect URI doesn't need to match the OAuth parameter, it's serialized in the query state,
|
||||
// to be used once the identification ends.
|
||||
var properties = new AuthenticationProperties { RedirectUri = returnUrl };
|
||||
return new ChallengeResult("Bearer", properties);
|
||||
return new ChallengeResult("Yavsc", properties);
|
||||
}
|
||||
|
||||
[HttpGet("~/signout")]
|
||||
public async Task<IActionResult> SignOut(string returnUrl="/") {
|
||||
await HttpContext.SignOutAsync("Bearer");
|
||||
await HttpContext.SignOutAsync("Yavsc");
|
||||
return Redirect(returnUrl);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -36,123 +36,38 @@ namespace testOauthClient.Controllers
|
|||
return View();
|
||||
}
|
||||
|
||||
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");
|
||||
|
||||
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, "http://dev.pschneider.fr/api/me");
|
||||
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", AccessToken);
|
||||
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("Index", model: await response.Content.ReadAsStringAsync());
|
||||
return View("UserInfo", model: await response.Content.ReadAsStringAsync());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#if FALSECODE
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> PostFiles(string subdir)
|
||||
{
|
||||
string results;
|
||||
_logger.LogInformation($"{Request.Form.Files.Count} file(s) to send");
|
||||
|
||||
// TODO better uri construction in production environment
|
||||
List<FormFile> args = new List<FormFile>();
|
||||
foreach (var formFile in Request.Form.Files)
|
||||
{
|
||||
_logger.LogWarning($"Treating {formFile.ContentDisposition}");
|
||||
MemoryStream memStream = new MemoryStream();
|
||||
const int sz = 1024 * 64;
|
||||
byte[] buffer = new byte[sz];
|
||||
using (var innerStream = formFile.OpenReadStream())
|
||||
{
|
||||
int szRead = 0;
|
||||
do
|
||||
{
|
||||
szRead = innerStream.Read(buffer, 0, sz);
|
||||
memStream.Write(buffer, 0, szRead);
|
||||
} while (szRead > 0);
|
||||
}
|
||||
memStream.Seek(0, SeekOrigin.Begin);
|
||||
args.Add(
|
||||
new FormFile(memStream, 0, formFile.Length, formFile.Name, formFile.Name )
|
||||
{
|
||||
ContentDisposition = formFile.ContentDisposition,
|
||||
ContentType = formFile.ContentType
|
||||
});
|
||||
}
|
||||
string uri = "http://dev.pschneider.fr/api/fs/" + System.Uri.EscapeDataString(subdir);
|
||||
_logger.LogInformation($"Posting data to '{uri}'...");
|
||||
|
||||
results = await RequestHelper.PostMultipart(uri, args.ToArray(), AccessToken);
|
||||
_logger.LogInformation("Data posted.");
|
||||
|
||||
return View("Index", model: results);
|
||||
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> PostDeviceInfo(CancellationToken cancellationToken)
|
||||
{
|
||||
/*
|
||||
using (var client = new HttpClient()) {
|
||||
var request = new HttpRequestMessage(HttpMethod.Post, "http://dev.pschneider.fr/api/gcm/register");
|
||||
|
||||
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", AccessToken);
|
||||
var json = JsonConvert.
|
||||
SerializeObject(new Yavsc.Models.Identity.GoogleCloudMobileDeclaration { DeviceId= "devid01", GCMRegistrationId = "1234" } );
|
||||
var content = new StringContent(json, Encoding.UTF8, "application/json");
|
||||
var response = await client.SendAsync(request, cancellationToken);
|
||||
response.EnsureSuccessStatusCode();
|
||||
|
||||
return View("Index", model: await response.Content.ReadAsStringAsync());
|
||||
}*/
|
||||
GCMRegistrationRecord result = null;
|
||||
var authHeader = $"Bearer {AccessToken}";
|
||||
_logger.LogWarning($"using authorization Header {authHeader}");
|
||||
try
|
||||
{
|
||||
|
||||
|
||||
using (var request = new SimpleJsonPostMethod(
|
||||
"http://dev.pschneider.fr/api/gcm/register", authHeader))
|
||||
{
|
||||
result = await request.Invoke<GCMRegistrationRecord>(new
|
||||
GCMRegistrationRecord
|
||||
{
|
||||
GCMRegistrationId = "testGoogleRegistrationIdValue",
|
||||
DeviceId = "TestDeviceId",
|
||||
Model = "TestModel",
|
||||
Platform = "External Web",
|
||||
Version = "0.0.1-rc1"
|
||||
});
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return View("Index", model: new { error = ex.Message });
|
||||
}
|
||||
return View("Index", model: result?.ToString());
|
||||
}
|
||||
#endif
|
||||
|
||||
protected string AccessToken
|
||||
{
|
||||
get
|
||||
{
|
||||
var claim = HttpContext.User?.FindFirst("access_token");
|
||||
if (claim == null)
|
||||
{
|
||||
throw new InvalidOperationException("no access_token");
|
||||
}
|
||||
|
||||
return claim.Value;
|
||||
}
|
||||
}
|
||||
|
||||
public IActionResult About()
|
||||
{
|
||||
ViewData["Message"] = "Your application description page.";
|
||||
|
|
@ -167,18 +82,6 @@ namespace testOauthClient.Controllers
|
|||
return View();
|
||||
}
|
||||
|
||||
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:6001/identity");
|
||||
|
||||
ViewBag.Json = JArray.Parse(content).ToString();
|
||||
return View("json");
|
||||
}
|
||||
|
||||
public IActionResult Logout()
|
||||
{
|
||||
return SignOut("Cookies", "oidc");
|
||||
|
|
|
|||
|
|
@ -21,8 +21,8 @@ using System.IdentityModel.Tokens.Jwt;
|
|||
.AddOpenIdConnect("Yavsc", options =>
|
||||
{
|
||||
options.Authority = "https://localhost:5001";
|
||||
|
||||
options.ClientId = "interactive";
|
||||
|
||||
options.ClientId = "mvc";
|
||||
options.ClientSecret = "49C1A7E1-0C79-4A89-A3D6-A37998FB86B0";
|
||||
options.ResponseType = "code";
|
||||
|
||||
|
|
|
|||
|
|
@ -2,6 +2,27 @@
|
|||
@{
|
||||
ViewData["Title"] = "Home Page";
|
||||
}
|
||||
@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) {
|
||||
|
|
|
|||
7
src/sampleWebAsWebApiClient/Views/Home/UserInfo.cshtml
Normal file
7
src/sampleWebAsWebApiClient/Views/Home/UserInfo.cshtml
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
@model string
|
||||
@{
|
||||
ViewData["Title"] = "User Info";
|
||||
}
|
||||
<h2>@ViewData["Title"].</h2>
|
||||
|
||||
<code>@Model</code>
|
||||
1
src/sampleWebAsWebApiClient/Views/Shared/json.cshtml
Normal file
1
src/sampleWebAsWebApiClient/Views/Shared/json.cshtml
Normal file
|
|
@ -0,0 +1 @@
|
|||
<pre>@ViewBag.Json</pre>
|
||||
|
|
@ -6,18 +6,13 @@
|
|||
}
|
||||
},
|
||||
"AllowedHosts": "*",
|
||||
"Authentication": {
|
||||
"Yavsc" : {
|
||||
"TokenEndpoint": "http://dev.pschneider.fr/connect/token",
|
||||
"AuthorizationEndpoint": "http://dev.pschneider.fr/connect/authorize",
|
||||
"ClientId": "interactive",
|
||||
"ClientSecret": "49C1A7E1-0C79-4A89-A3D6-A37998FB86B0"
|
||||
}
|
||||
},
|
||||
"Kestrel": {
|
||||
"Endpoints":
|
||||
{
|
||||
"http": {
|
||||
"Http": {
|
||||
"Url": "http://localhost:5002"
|
||||
},
|
||||
"Https": {
|
||||
"Url": "https://localhost:5003"
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,13 +1,19 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Yavsc.Abstract\Yavsc.Abstract.csproj" />
|
||||
<PackageReference Include="IdentityModel.AspNetCore" Version="4.3.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="IdentityModel.AspNetCore" Version="4.3.0" />
|
||||
<Content Update="Views\Shared\Json.cshtml">
|
||||
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
|
||||
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
|
||||
</Content>
|
||||
</ItemGroup>
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
</PropertyGroup>
|
||||
</Project>
|
||||
|
||||
</Project>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue