.Net Core Identity 2 Provider login Cancel leads to unhandled exception

billy jean

I've added LinkedIn as a provider. I have implemented the login and register with LinkedIn without any issue. In the use case where the user CANCELS from within the provider Pages (either linkedIn login or cancels the authorization of the app) the identity middleware seems to throw an unhandled exception:

An unhandled exception occurred while processing the request.
Exception: user_cancelled_login;Description=The user cancelled LinkedIn login
Unknown location

Exception: An error was encountered while handling the remote login.


System.Exception: user_cancelled_login;Description=The user cancelled LinkedIn login

Exception: An error was encountered while handling the remote login.

The provider setup in startup defines the callback:

services.AddAuthentication().AddOAuth("LinkedIn", "LinkedIn", c =>
            c.ClientId = Configuration["Authentication:LinkedIn:ClientId"];
            c.ClientSecret = Configuration["Authentication:LinkedIn:ClientSecret"];
            c.CallbackPath = "/signin-linkedin";

And As I have said the middleware seems to handled ALL other cases except where the user cancels within the LinkedIn pages. The return URL from LinkedIn looks correct:


But never gets to my ExternalCallback controller method where the other cases like successful login/authorization are handled??

I'm wondering if this is working for anyone else with 3rd part providers?

Kirk Larkin

There's a Github issue that explains what's happening here in more detail, with a bit of information as to why it's happening and even an indication that this won't be "fixed":

Handling the RemoteFailure event is the right thing to do. We should update our docs/samples to show how to handle that event and at least show a more appropriate message to the user. The error page sample could include a link to enabled the user to try logging in again.

Unfortunately it's difficult to implement this event in a very generic way that's also super useful because each remote auth provider has its own behavior for different types of failures.

The workaround for this (as quoted above) is to handle the RemoteFailure event:

services.AddAuthentication().AddOAuth("LinkedIn", "LinkedIn", c => {
    // ...
    c.Events.OnRemoteFailure = ctx =>
        // React to the error here. See the notes below.
        return Task.CompletedTask;
    // ...

ctx is an instance of RemoteFailureContext, which includes an Exception property describing what went wrong. ctx also contains a HttpContext property, allowing you to perform redirects, etc, in response to such exceptions.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at


Login to comment


TOP Ranking