Asp.net

ASP.NET MVC 未處理 OpenIdConnect signin-oidc 路由

  • September 4, 2020

我正在使用外部 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,
   });
}

當我在我的頁面上點擊登錄時,我被重定向到我的身份驗證提供程序,並且正確的 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");
   }

   ... 
}

但是,在我成功通過我的外部提供商進行身份驗證並被重定向到我的網站(目前它只是http://localhost:8888/signin-oidc用於開發目的)之後,該路由不會被處理。我得到了一個 404,所以顯然有些事情沒有像它應該做的那樣工作。

我已經安裝了 ELMAH,這會報告以下異常消息:

System.Web.HttpException (0x80004005):找不到路徑“/signin-oidc”的控制器或未實現 IController。

對於上下文:這同樣適用於 ASP.NET Core API,使用具有相同配置的相同外部 openid 提供程序。

對於將來瀏覽此內容的任何人,這就是答案:

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

引用自:https://stackoverflow.com/questions/56471854