openiddict-core icon indicating copy to clipboard operation
openiddict-core copied to clipboard

Question: How to handle OpenIdConnectProtocolException when user denies resource access

Open pealmeid opened this issue 9 years ago • 13 comments

In the samples Mvc.Server/Mvc.Client, if the user signs on the server but denies resource access, the server returns a 500 error code and the client fails with the following message:

An unhandled exception occurred while processing the request. OpenIdConnectProtocolException: Message contains error: 'access_denied', error_description: 'The authorization grant has been denied by the resource owner.', error_uri: 'ErrorUri null'.

I read issues https://github.com/aspnet-contrib/AspNet.Security.OpenIdConnect.Server/issues/138 and https://github.com/aspnet/Security/issues/710 and I understand this happens because UseOpenIdConnectAuthentication (unlike UseBattleNetAuthentication, mentioned in the thread for https://github.com/aspnet/Security/issues/710) does not have an option to handle remote errors.

Is my understanding correct? And, if so, what is the suggested/recommended way to handle this (in an app similar to the samples)? Thanks in advance!

pealmeid avatar Jan 18 '17 11:01 pealmeid

Is my understanding correct?

Yep.

And, if so, what is the suggested/recommended way to handle this (in an app similar to the samples)?

You can use the RemoteFailure event to override the default logic:

app.UseOpenIdConnectAuthentication(new OpenIdConnectOptions {
    // ...

    Events = new OpenIdConnectEvents {
        OnRemoteFailure = context => {
            context.Response.Redirect("/");
            context.HandleResponse();

            return Task.FromResult(0);
        }
    }
});

kevinchalet avatar Jan 18 '17 14:01 kevinchalet

Perfect! Once again, thanks so much for your help!

pealmeid avatar Jan 18 '17 23:01 pealmeid

It says "UseOpenIdConnectAuthentication" is obsolete !

deepakkumpala avatar Aug 25 '18 07:08 deepakkumpala

In the ConfigureServices method add;

services.AddOpenIdConnect("oidc", options => {
    options.SignInScheme = "Cookies";
    options.Authority = "https://localhost:5000";
    options.RequireHttpsMetadata = false;
    options.ClientId = "mvc";
    options.SaveTokens = true;

    options.Events = new OpenIdConnectEvents {
        OnRemoteFailure = context => {
            context.Response.Redirect("/");
            context.HandleResponse();

            return Task.FromResult(0);
        }
    };
});

You will need to add the Microsoft.AspNetCore.Authentication.OpenIdConnect and System.Threading.Tasks namespaces.

mkellsy avatar Oct 23 '18 16:10 mkellsy

What a great message thread! It's 2020 now and in case it would help someone - there is a new event available in the OpenIdProvider options (as of ASP.NET Core 3.0) specifically for the "access_denied" responses named "OnAccessDenied" (the signature and handler are the same as OnRemoteFailure). This is fantastic news because "access_denied" isn't technically a "remote failure". The docs are as follows in case you need it: https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.authentication.remoteauthenticationevents.onaccessdenied?view=aspnetcore-3.0

timmi4sa avatar Dec 11 '20 23:12 timmi4sa

Thanks @timmi4sa for that suggestion. Your link seems to click to an issue board. Here are the docs for "OnAccessDenied": https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.authentication.remoteauthenticationevents.onaccessdenied?view=aspnetcore-5.0

uipathswapnil avatar Jan 06 '21 19:01 uipathswapnil

can somebody tell me what is the reason of returning Task.FromResult(0) instead of Task.CompletedTask?

a-a-k avatar Sep 21 '22 04:09 a-a-k

The event OnAccessDenied does not get triggered when the user declines the consent dialog. When the user declines that dialog, the OnRemoteFailure event is what triggers, just as @mkellsy described above. @timmi4sa, I don't think OnAccessDenied is an answer to @pealmeid's question.

kandaker avatar Sep 26 '23 22:09 kandaker

@kandaker

The event OnAccessDenied does not get triggered when the user declines the consent dialog. When the user declines that dialog, the OnRemoteFailure event is what triggers, just as @mkellsy described above. @timmi4sa, I don't think OnAccessDenied is an answer to @pealmeid's question.

Perhaps the token server you are using is returning some error other than ‘access_denied’?

timmi4sa avatar Sep 27 '23 06:09 timmi4sa

@kandaker Perhaps the token server you are using is returning some error other than ‘access_denied’?

That makes a lot of sense. I tried this against Microsoft AAD/EntraID. For that interaction, the way to get a good user experience when the user declines consent, is through the OnRemoteFailure event. But perhaps with Google, Okta, or others, it's with OnAccessDenied. OpenID Connect interop is always an adventure because of all the little implementation differences between the directory providers.

kandaker avatar Sep 27 '23 15:09 kandaker

Is my understanding correct?

Yep.

And, if so, what is the suggested/recommended way to handle this (in an app similar to the samples)?

You can use the RemoteFailure event to override the default logic:

app.UseOpenIdConnectAuthentication(new OpenIdConnectOptions {
    // ...

    Events = new OpenIdConnectEvents {
        OnRemoteFailure = context => {
            context.Response.Redirect("/");
            context.HandleResponse();

            return Task.FromResult(0);
        }
    }
});

@kevinchalet I know this issue is a bit old, but why would one not use OnAccessDenied. That seems more specific rather than using a more generic OnRemoteFailure?

SangeetAgarwal avatar Oct 26 '23 14:10 SangeetAgarwal

@SangeetAgarwal well, this thread predates the introduction of the OnAccessDenied event (I contributed it in 2018: https://github.com/aspnet/Security/pull/1887), so it was pretty much the only option in 2017. If you're only using the event to redirect access_denied errors, just setting AccessDeniedPath in the options is a much simpler approach.

It's worth noting that the OpenIddict client is now my recommended option. It offers a native status codes middleware integration that allows handling all types of OIDC errors more gracefully, including access_denied.

kevinchalet avatar Oct 26 '23 15:10 kevinchalet

@kandaker Perhaps the token server you are using is returning some error other than ‘access_denied’?

That makes a lot of sense. I tried this against Microsoft AAD/EntraID. For that interaction, the way to get a good user experience when the user declines consent, is through the OnRemoteFailure event. But perhaps with Google, Okta, or others, it's with OnAccessDenied. OpenID Connect interop is always an adventure because of all the little implementation differences between the directory providers.

It is probably because AAD returns consent_required instead of access_denied:

OpenIdConnectProtocolException: Message contains error: 'consent_required', error_description: 'AADSTS65004: User declined to consent to access the app. error_uri: 'https://login.microsoftonline.com/error?code=65004'.

FranklinWhale avatar Dec 07 '23 13:12 FranklinWhale