Skip to content

Instantly share code, notes, and snippets.

View ghstahl's full-sized avatar
💭
coding always coding

Herb Stahl ghstahl

💭
coding always coding
  • Santa Monica, CA
View GitHub Profile
public interface ICustomTokenRequestManager
{
void AddTokenRequestFunction(string key, Func<ManagedToken, IServiceProvider,IOAuth2CredentialManager, CancellationToken, Task<ManagedToken>> func);
Func<ManagedToken, IServiceProvider,IOAuth2CredentialManager, CancellationToken, Task<ManagedToken>> GetTokenRequestFunc(string key);
}
static async Task<ManagedToken> ExecuteClientCredentialsRequestAsync(
ManagedToken managedToken,
IServiceProvider serviceProvider,
IOAuth2CredentialManager oAuth2CredentialManager,
CancellationToken cancellationToken = default)
{
var creds = await oAuth2CredentialManager.GetOAuth2CredentialsAsync(managedToken.CredentialsKey);
var client = new HttpClient();
var response = await client.RequestClientCredentialsTokenAsync(
new ClientCredentialsTokenRequest
var oAuth2CredentialManager = serviceProvider.GetRequiredService<IOAuth2CredentialManager>();
// Register the credentials for my test OAuth2 service
oAuth2CredentialManager.AddCredentialsAsync("test", new OAuth2Credentials
{
Authority = "https://demo.identityserver.io",
ClientId = "m2m",
ClientSecret = "secret"
}).GetAwaiter().GetResult();
// Next register the token request. In this case its a simple client_credentials call
var globalTokenManager = serviceProvider
.GetRequiredService<ITokenManager<GlobalDistributedCacheTokenStorage>>();
globalTokenManager.AddManagedTokenAsync("test", new ManagedToken
{
CredentialsKey = "test",
RequestFunctionKey = "client_credentials",
RequestedScope = null // everything
}).GetAwaiter().GetResult();
public interface ITokenManager<T> where T : TokenStorage
{
Task<ManagedToken> AddManagedTokenAsync(string key, ManagedToken tokenConfig);
Task RemoveManagedTokenAsync(string key);
Task<ManagedToken> GetManagedTokenAsync(string key, bool forceRefresh = false, CancellationToken cancellationToken = default);
Task RemoveAllManagedTokenAsync();
}
var baseAddress = builder.HostEnvironment.BaseAddress;
var uri = new Uri(baseAddress);
baseAddress = $"{uri.Scheme}://{uri.Authority}";
builder.Services.AddHttpClient(
Constants.HostingHttpClientName,
client => {client.BaseAddress = new Uri(baseAddress);})
.AddHttpMessageHandler<AuthorizedHandler>();
public class AccountHelper
{
private const string LogInPath = "Identity/Account/Login";
private const string LogOutPath = "Identity/Account/Logout";
private readonly NavigationManager _navigation;
private readonly IHostAccessor _hostAccessor;
private readonly ILogger<AccountHelper> _logger;
public AccountHelper(
NavigationManager navigation,
IHostAccessor hostAccessor,
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request,CancellationToken cancellationToken)
{
HttpResponseMessage responseMessage =
await base.SendAsync(request, cancellationToken);
if (responseMessage.StatusCode == HttpStatusCode.Unauthorized ||
responseMessage.StatusCode == HttpStatusCode.Forbidden)
{
// if server returned 401 Unauthorized, redirect to login page
_accountHelper.SignIn();
return null;
[ApiController]
[Route("api/[controller]")]
[Produces("application/json")]
public class AuthStatusController : ControllerBase
{
private readonly ILogger<AuthStatusController> _logger;
public AuthStatusController(ILogger<AuthStatusController> logger)
{
_logger = logger;
}
public class AuthenticationPeekMiddleware
{
private readonly RequestDelegate _next;
private readonly AuthenticationPeekOptions _options;
public AuthenticationPeekMiddleware(RequestDelegate next,
IOptions<AuthenticationPeekOptions> options)
{
_next = next ?? throw new ArgumentNullException(nameof(next));
_options = options.Value;
}