Web API client connected

This commit is contained in:
Paul Schneider 2024-11-12 08:32:15 +00:00
commit 716bfb2e05
23 changed files with 225 additions and 339 deletions

View file

@ -1,9 +1,8 @@

using System.Threading.Tasks;
using Microsoft.AspNetCore.Http.Authentication;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Mvc;
namespace Mvc.Client.Controllers {
public class AuthenticationController : Controller {
[HttpGet("~/signin")]
@ -13,14 +12,13 @@ namespace Mvc.Client.Controllers {
// 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("Yavsc", properties);
return new ChallengeResult("Bearer", properties);
}
[HttpGet("~/signout")]
public async Task<IActionResult> SignOut(string returnUrl="/") {
await HttpContext.Authentication.SignOutAsync("Bearer");
await HttpContext.SignOutAsync("Bearer");
return Redirect(returnUrl);
}
}
}

View file

@ -1,14 +1,15 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Yavsc.Server.Helpers;
using Yavsc.Server.Model;
using Newtonsoft.Json.Linq;
namespace testOauthClient.Controllers
{
@ -52,6 +53,7 @@ namespace testOauthClient.Controllers
}
#if FALSECODE
[HttpPost]
public async Task<IActionResult> PostFiles(string subdir)
{
@ -77,11 +79,10 @@ namespace testOauthClient.Controllers
}
memStream.Seek(0, SeekOrigin.Begin);
args.Add(
new FormFile
new FormFile(memStream, 0, formFile.Length, formFile.Name, formFile.Name )
{
ContentDisposition = formFile.ContentDisposition,
ContentType = formFile.ContentType,
Stream = memStream
ContentType = formFile.ContentType
});
}
string uri = "http://dev.pschneider.fr/api/fs/" + System.Uri.EscapeDataString(subdir);
@ -137,6 +138,7 @@ namespace testOauthClient.Controllers
}
return View("Index", model: result?.ToString());
}
#endif
protected string AccessToken
{
@ -165,9 +167,27 @@ 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");
}
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
return View();
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
}
}