In my Blazor webapp application I am trying to implement openIDConnect to connect to a remote Authentication API hosting identityServer4. When this API is up my application redirects to the authorities’ login page and returns and I can access pages with [Authorize] as expected.
But when the authority is down my application crashes when trying to access any page with a [Authorize] attribute; I would prefer Blazor to handle this exception and redirect to an error page
Exception:
SocketException: No connection could be made because the target machine actively refused it.
System.Net.Sockets.Socket+AwaitableSocketAsyncEventArgs.ThrowException(SocketError error, CancellationToken cancellationToken)
HttpRequestException: No connection could be made because the target machine actively refused it. (localhost:18100)
System.Net.Http.HttpConnectionPool.ConnectToTcpHostAsync(string host, int port, HttpRequestMessage initialRequest, bool async, CancellationToken cancellationToken)
IOException: IDX20804: Unable to retrieve document from: '[PII of type 'System.String' is hidden. For more details, see https://aka.ms/IdentityModel/PII.]'.
Microsoft.IdentityModel.Protocols.HttpDocumentRetriever.GetDocumentAsync(string address, CancellationToken cancel)
InvalidOperationException: IDX20803: Unable to obtain configuration from: 'https://localhost:18100/.well-known/openid-configuration'. Will retry at '22/05/2024 1:11:26 AM +00:00'. Exception: 'System.IO.IOException: IDX20804: Unable to retrieve document from: '[PII of type 'System.String' is hidden. For more details, see https://aka.ms/IdentityModel/PII.]'.
---> System.Net.Http.HttpRequestException: No connection could be made because the target machine actively refused it. (localhost:18100)
---> System.Net.Sockets.SocketException (10061): No connection could be made because the target machine actively refused it.
at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.ThrowException(SocketError error, CancellationToken cancellationToken)
at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.System.Threading.Tasks.Sources.IValueTaskSource.GetResult(Int16 token)
at System.Net.Sockets.Socket.<ConnectAsync>g__WaitForConnectWithCancellation|285_0(AwaitableSocketAsyncEventArgs saea, ValueTask connectTask, CancellationToken cancellationToken)
OpenIDConnect setup
builder.Services.AddAuthentication(options =>
{
options.DefaultScheme = "Cookies";
options.DefaultChallengeScheme = "oidc";
}).AddOpenIdConnect("oidc", options =>
{
options.Authority = "https://localhost:18100";
options.ClientId = "client_id";
options.ResponseType = "code";
options.SaveTokens = true;
options.GetClaimsFromUserInfoEndpoint = true;
options.ClaimActions.MapAllExcept("iss", "nbf", "exp", "aud", "nonce", "iat", "c_hash", "s_hash", "at_hash", "displayname", "givennames", "familyname", "roles");
options.ClaimActions.MapJsonKey("role", "roles");
options.ClaimActions.MapUniqueJsonKey("family_name", "familyname");
options.ClaimActions.MapUniqueJsonKey("given_name", "givennames");
options.ClaimActions.MapUniqueJsonKey("name", "displayname");
options.SignOutScheme = "oidc";
options.UseTokenLifetime = false;
options.Scope.Add("openid");
options.Scope.Add("profile");
options.TokenValidationParameters = new TokenValidationParameters
{
NameClaimType = "name",
RoleClaimType = "role"
};
options.SignedOutCallbackPath = "/signout-callback-oidc";
options.SignedOutRedirectUri = "/ui";
}).AddCookie();
builder.Services.AddAuthorization();
I’ve tried adding OpenIdConnectEvents like OnAuthenticationFailed, OnRemoteFailed and none has worked so far, am I missing something obvious?
user25177079 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.