ASP.NET MVC 不处理 OpenIdConnect signin-oidc 路由

6 asp.net asp.net-mvc openid-connect

我正在使用外部 OIDC 身份提供商将我的用户登录到我的网上商店。该网店基于 ASP.NET MVC 和 .NET Framework 4.7.2 构建。

我已经开始使用基本的 MVC 模板并添加我的身份验证代码。

public void ConfigureAuth(IAppBuilder app)
{

    JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();// = new Dictionary<string, string>();

    app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);

    app.UseCookieAuthentication(new CookieAuthenticationOptions
    {
        AuthenticationType = CookieAuthenticationDefaults.AuthenticationType
    });

    var authority = "https://authentication.myOpenIdProvider.com/auth/oauth2/realms/root/realms/test";
    var redirectUri = "http://localhost:8888/signin-oidc";
    var postlogoUri = "http://localhost:8888/signout-callback-oidc";
    var clientId = "MyClientId";
    var clientSecret = "MyClientSecret";

    app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
    {
        ClientId = clientId,
        ClientSecret = clientSecret,
        Authority = authority,
        RedirectUri = redirectUri,
        PostLogoutRedirectUri = postlogoUri,
        ResponseType = "code",
        Scope = "openid favorites",
        SignInAsAuthenticationType = "Cookies",
        RequireHttpsMetadata = false,
    });
}
Run Code Online (Sandbox Code Playgroud)

当我在我的页面上点击登录时,我被重定向到我的身份验证提供程序,同时传递了正确的 redirectUri。

public class AccountController : Controller
{
    public ActionResult Login()
    {
        if (!HttpContext.User.Identity.IsAuthenticated)
        {
            HttpContext.GetOwinContext().Authentication.Challenge(OpenIdConnectAuthenticationDefaults.AuthenticationType);
            return new HttpUnauthorizedResult();
        }

        return RedirectToAction("Index", "Home");
    }

    ... 
}
Run Code Online (Sandbox Code Playgroud)

但是,在我成功地通过我的外部提供商进行身份验证并被重定向到我的站点(目前它只是用于开发目的的http://localhost:8888/signin-oidc)之后,该路由没有被处理。我收到了 404,所以很明显有些事情没有像它应该做的那样工作。

我已经安装了 ELMAH 并且报告了以下异常消息:

System.Web.HttpException (0x80004005): 路径“/signin-oidc”的控制器未找到或未实现 IController。

对于上下文:同样适用于 ASP.NET Core API,使用具有相同配置的相同外部 openid 提供程序。

小智 6

对于将来浏览此内容的任何人,这就是答案:

Owin.OpenIdConnect不支持"code"ResponseTypes。你也需要设置"id_token"。如果由于任何原因你不能这样做,你基本上需要自己实现部分规范(主要是通过连接到MessageReceived通知事件)。

在 OpenIdConnect Handler 的源代码中查看这部分:

https://github.com/aspnet/AspNetKatana/blob/0f6dc4bf2722fb08759da3eacaf38f2a098771bd/src/Microsoft.Owin.Security.OpenIdConnect/OpenidConnectAuthenticationHandler.cs#L258-L264

  • 版本 4.1.0 现已支持“授权代码流程” (2认同)
  • 但是,当我在通知参数中拥有可用的身份验证票证时,我仍然必须处理“MessageReceived”通知并获取“id_token”、“access_token”和“refresh_token”,并在“AuthorizationCodeReceived”通知中使用这些值,我不确定这是否是好方法但它在使用 owin 库版本 4.1.0 进行授权 cdoe 流程时有效 (2认同)