Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem with OpenIdConnectAuthenticationOptions in Startup.cs (AuthenticationFailed property)

I have the following code for my Startup.cs

public void ConfigureAuth(IAppBuilder app)
{
    app.UseWindowsAzureActiveDirectoryBearerAuthentication(
        new WindowsAzureActiveDirectoryBearerAuthenticationOptions
        {
            TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters()
            {
                ValidAudience = ConfigurationManager.AppSettings["value1"]
            },
            Tenant = ConfigurationManager.AppSettings["value2"]
        });

    app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);

    app.UseCookieAuthentication(new CookieAuthenticationOptions
    {
        CookieManager = new SystemWebCookieManager()
    });

    app.UseKentorOwinCookieSaver(); //Workaround for infinite loop between webapp & login page

    app.UseOpenIdConnectAuthentication(
        new OpenIdConnectAuthenticationOptions
        {
            ClientId = clientId,
            Authority = Authority,
            PostLogoutRedirectUri = redirectUri,
            RedirectUri = redirectUri,

            Notifications = new OpenIdConnectAuthenticationNotifications()
            {
                //
                // If there is a code in the OpenID Connect response, redeem it for an access token and refresh token, and store those away.
                //
                AuthorizationCodeReceived = OnAuthorizationCodeReceived,
                AuthenticationFailed = OnAuthenticationFailed
            }
        });
}

private Task OnAuthenticationFailed(AuthenticationFailedNotification<OpenIdConnectMessage, OpenIdConnectAuthenticationOptions> context)
{
    context.HandleResponse();
    context.Response.Redirect("/Home/Error?message=" + context.Exception.Message);
    return Task.FromResult(0);
}

However, when I do this

AuthenticationFailed = OnAuthenticationFailed

I am getting the following error: Error CS0123 No overload for 'OnAuthenticationFailed' matches delegate 'Func< AuthenticationFailedNotification< OpenIdConnectMessage, OpenIdConnectAuthenticationOptions>, Task>'

I don't see why this is happening since the types match here. It all started happening since I updated to Owin 4.0.1 as well as all the Microsoft.IdentityModel and System.IdentityModel to 5.4.0.

I know there were some breaking changes in the version 5.X but I think there are all resolved on version 5.4.0 and this is the only issue I have left.

like image 575
user3587624 Avatar asked Feb 05 '19 02:02

user3587624


2 Answers

I had the same issue. After updating Microsoft.IdentityModel the type OpenIdConnectMessage is in a different namespace:

Microsoft.IdentityModel.Protocols.OpenIdConnect
like image 74
Gijs Avatar answered Nov 09 '22 23:11

Gijs


Option 1: use in-line with a lambda anonymous code block

Option 2: For a cleaner separation of concerns:

Use explicit namespace like this

using Microsoft.IdentityModel.Protocols.OpenIdConnect;

Instead of using

Microsoft.IdentityModel.Protocols;

No other change required

like image 30
Renjish Alphonse Avatar answered Nov 10 '22 00:11

Renjish Alphonse