使用 OWIN/OIDC 针对 Azure AD B2C 验证自定义 Umbraco 控制器

Authenticating custom Umbraco controller with OWIN/OIDC against Azure AD B2C

我卡住了。

我有一个自定义的 Umbraco 控制器,它继承自 RenderMvcController,当 Index() 操作方法被点击时,会返回一个文件。这可行,但我想做的是通过用 AuthorizeAttribute 装饰操作然后要求用户进行身份验证来保护它。

namespace MyNamespace.Controllers
{
    public class MyModelController : RenderMvcController
    {
        [Authorize]
        public ActionResult Index(RenderModel model)
        {
            // ...
        }
    }
}

将使用 OWIN 和 OpenId Connect 针对 Azure AD B2C 应用程序进行身份验证。这也有效并经过测试,但在非 Umbraco 上下文中。

我已经阅读了许多与该主题相关的线程和代码,但我正在努力将它集成到 Umbraco 中。我有一个自定义启动 class,它继承自 UmbracoDefaultOwinStartup。我向我的 AuthController 注册自定义路由并通过 IAppBuilder.UseOpenIdConnectAuthentication().

配置 OIDC

但我需要 Umbraco 胶水并且在理解我应该如何配置 cookie 时遇到问题。我已检查启动 Configuration() 方法是否被调用。

namespace MyNamespace
{
    public class CustomOwinStartup : UmbracoDefaultOwinStartup
    {
        public override void Configuration(IAppBuilder app)
        {
            base.Configuration(app);
            ConfigureAuth(app);

            RouteTable.Routes.MapRoute(
                "CustomAuth",
                "CustomAuth/{action}",
                new { controller = "Auth" }
            );
        }

        private void ConfigureAuth(IAppBuilder app)
        {
            app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
            app.UseCookieAuthentication(new CookieAuthenticationOptions
            {
                AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
                LoginPath = new PathString("/CustomAuth/SignUpSignIn") // TODO: What should I put here?
            });
            app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
            app.UseOpenIdConnectAuthentication(
                // Passing options that are tested and working
            );
        }
    }
}

然后我有了我的 auth 控制器,目前它非常简单。我不需要与 Umbraco 用户同步授权信息。

namespace MyNamespace.Controllers
{
    public partial class CustomAuthController : Controller
    {
        public CustomAuthController() : base()
        {
        }

        public void SignUpSignIn()
        {
            if (!Request.IsAuthenticated)
            {
                HttpContext.GetOwinContext().Authentication.Challenge();
                return;
            }

            Response.Redirect("/"); // TODO: Maybe this should redirect me back to original route MyModel/Index in some way
        }
    }
}

如果我 运行 这并尝试通过我的属性装饰的自定义 Umbraco 控制器我得到这个错误:

Page not found

No umbraco document matches the url '/login.aspx?ReturnUrl=MYORIGINALROUTEHTTPENCODED'. This page can be replaced with a custom 404. Check the documentation for "custom 404".

我猜测这是因为Web.config中的<authentication mode="Forms">设置,但是如果我删除它或者将属性mode设置为"None",这会不会不影响后台登录?

如果有人能帮我指出正确的方向,非常感谢!

不幸的是,我没有足够的时间来设置相同的环境并检查所有内容,但我会给出一些想法和 post 几个链接。

要覆盖后台登录逻辑,您不需要创建自定义授权控制器或覆盖登录页面。由于 Umbraco 使用 ASP.NET Identity 进行授权,因此您需要在 OWIN 启动时正确配置它,它将根据您的需要工作。然后您将使用 Umbraco 授权工具来检查用户是否可以点击 MyModelController 的 Index 操作。

如果您需要实现自定义 username/password,请检查是否使用 Umbraco 扩展。查看更多:https://our.umbraco.org/documentation/Reference/Security/#replacing-the-basic-username-password-check

您还可以为 Active Directory 使用 Umbraco 扩展。查看更多:https://our.umbraco.org/documentation/Reference/Security/#authenticating-with-active-directory-credentials

关于 AuthorizeAttribute。您会收到 not found 错误,因为 Authorize 对 OWIN 和 Umbraco 一无所知。它重定向到默认路径 login.aspx 页面,它在项目中不存在。您可以将 <authentication mode="Forms"> 设置为 None。它不会影响 Umbraco 后台,因为它使用 ASP.NET 身份。如果你想保护一些资源,你可以使用 UmbracoAuthorizeAttribute。

查看更多:https://our.umbraco.org/documentation/Implementation/Controllers/#mvc

无论如何都要阅读更多 Umbraco 教程和文档。我希望你能找到答案。欢迎提问。

已更新

尝试使用您自己的 AuthorizeAttribute 版本。有一个例子:http://www.c-sharpcorner.com/UploadFile/56fb14/custom-authorization-in-mvc/

您需要重写 AuthorizeCore 方法并且 return 需要来自 HandleUnauthorizedRequest 的结果。所以你可以 return RedirectResult 到你的登录页面或任何你需要的。

您可以将其应用为

    [CustomAuthorize]
    public ActionResult Index(RenderModel model)
    {
        // ...
    }

已更新 2

如果自定义 AuthorizeAttribute 没有帮助或不起作用,您可以创建 ActionFilter 并将授权逻辑放在那里。当然,这不是最佳做法,但您至少可以尝试这样做。看这里:https://msdn.microsoft.com/en-us/library/gg416513(vs.98).aspx#Anchor_2