This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ApiController] | |
[Route("api/[controller]")] | |
[Produces("application/json")] | |
public class AuthStatusController : ControllerBase | |
{ | |
private readonly ILogger<AuthStatusController> _logger; | |
public AuthStatusController(ILogger<AuthStatusController> logger) | |
{ | |
_logger = logger; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |