OpenId 连接 Owin 中间件与 Orchard CMS 1.9.2

OpenId Connect Owin Middleware with Orchard CMS 1.9.2

我正在尝试接管 Orchard 管理员身份验证流程,并将其替换为使用 Azure AD 的 OpenId Connect(用于 SSO 目的)。在用户使用 OpenId 进行身份验证后,我想将用户登录到 Orchard 以使用 Orchard 的角色进行授权。

这是我目前的代码

OwinMiddlewareProvider:

public class OpenIdConnectMiddleware : IOwinMiddlewareProvider
{
    public IEnumerable<OwinMiddlewareRegistration> GetOwinMiddlewares()
    {
        return new[]
        {
            new OwinMiddlewareRegistration
            {
                Priority = "1",

                Configure = app => {
                    app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);

                    app.UseCookieAuthentication(new CookieAuthenticationOptions());

                    app.UseOpenIdConnectAuthentication(
                        new OpenIdConnectAuthenticationOptions
                        {
                            ClientId = "<My Client Id>",
                            Authority = "<My Authority>",
                            RedirectUri = "https://localhost:44357/"
                        });
                }
            }
        };
    }
}

我接管默认拒绝访问路由的路由:

new RouteDescriptor {
    Priority = 1,
    Route = new Route(
        "Users/Account/AccessDenied",
        new RouteValueDictionary {
                                {"area", "BCS.Authentication"},
                                {"controller", "Account"},
                                {"action", "LogOn"}
        },
        new RouteValueDictionary (),
        new RouteValueDictionary {
                                {"area", "BCS.Authentication"}
        },
        new MvcRouteHandler())
    }

最后,我的 AccountController 具有以下内容:

public ActionResult LogOn(string returnUrl)
{
    if (!Request.IsAuthenticated)
    {
        HttpContext.GetOwinContext().Authentication.Challenge(
            new AuthenticationProperties { RedirectUri = string.IsNullOrEmpty(returnUrl) ? "/" : returnUrl },
            OpenIdConnectAuthenticationDefaults.AuthenticationType);

        return new EmptyResult();
    }

    var currentUser = _authenticationService.GetAuthenticatedUser();

    if (currentUser == null)
    {
        // temporary, just for testing purposes
        var user = _membershipService.GetUser("douwinga");
        _authenticationService.SignIn(user, false);
    }

    return new RedirectResult(returnUrl);
}

当我导航到 https://localhost:44357/admin Request.IsAuthenticated 时使用此代码是错误的,因此它将我带到我们的 ADFS 登录页面。登录后,我将返回到登录操作,现在 Request.IsAuthenticated 为真。然后它尝试使用 authenticationService 来查看我是否通过了 Orchard 的身份验证并且显然 returns null,所以它然后将我登录到 Orchard。如果我检查 authenticationService 以查看我是否在登录后登录,它 returns 我的用户,所以此时我通过 Orchard 进行身份验证。

那么问题来了。我现在被重定向到/admin,因为我已经通过身份验证并且它会进行另一次身份验证检查,所以我被发送回我的控制器的登录操作。这次 Request.IsAuthenticated 仍然是 true,但是 currentUser 仍然是 null,所以我不再通过 Orchard 进行身份验证,所以它再次尝试登录 Orchard。这让我陷入了困境。

有什么我遗漏的吗?有没有更好的方法?

Radio Systems 一直在开发一个新模块。它也在 Microsoft 内部使用,并将尽快包含在 Orchard 中

https://github.com/RadioSystems/RadioSystems.AzureAuthentication