Startup.cs中的OpenIdConnectAuthenticationOptions问题(AuthenticationFailed属性)

时间:2019-02-05 02:11:44

标签: c# authentication owin openid

我的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);
}

但是,当我这样做

  

AuthenticationFailed = OnAuthenticationFailed

我遇到以下错误:错误CS0123'OnAuthenticationFailed'没有重载匹配委托'Func ,Task>'

我不明白为什么会这样,因为这里的类型匹配。自从我更新到Owin 4.0.1以及所有Microsoft.IdentityModel和System.IdentityModel到5.4.0以来,一切都开始发生。

我知道版本5.X中有一些重大更改,但是我认为版本5.4.0中的所有问题都已解决,这是我剩下的唯一问题。

2 个答案:

答案 0 :(得分:1)

我有同样的问题。更新Microsoft.IdentityModel后,类型OpenIdConnectMessage在另一个命名空间中:Microsoft.IdentityModel.Protocols。 OpenIdConnect

干杯, Gijs Stoeldraaijers

答案 1 :(得分:0)

选项1:  与lambda匿名代码块一起在线使用

选项2:  为了更清晰地分离关注点:

使用这样的显式命名空间

using Microsoft.IdentityModel.Protocols.OpenIdConnect;

代替使用

Microsoft.IdentityModel.Protocols;

不需要其他更改