【问题标题】:Integrate Auth0 with Umbraco 7 for member authentication将 Auth0 与 Umbraco 7 集成以进行成员身份验证
【发布时间】:2016-07-03 05:32:17
【问题描述】:

我想将Auth0 与 Umbraco 7 集成以进行成员身份验证(成员是公共网站的用户,而不是后端 CMS 用户)。

将两者整合需要哪些步骤?

【问题讨论】:

    标签: c# authentication umbraco umbraco7 auth0


    【解决方案1】:

    为了一个干净的解决方案,我创建了一个空的 ASP.NET MVC 项目并使用 NuGet 添加了 Umbraco。我还使用 NuGet 引入了 Auth0。

    1) 覆盖 UmbracoDefaultOwinStartup

    将 Startup.cs 添加到解决方案中,继承自 UmbracoDefaultOwinStartup,这样我们仍然可以让 Umbraco 做这件事:

    using Microsoft.AspNet.Identity;
    using Microsoft.Owin;
    using Microsoft.Owin.Security.Cookies;
    using Owin;
    using System.Configuration;
    using System.Web.Mvc;
    using System.Web.Routing;
    
    [assembly: OwinStartup("MyStartup", typeof(MySolution.MyStartup))]
    namespace MySolution
    {
        public class MyStartup : Umbraco.Web.UmbracoDefaultOwinStartup
        {
            public override void Configuration(IAppBuilder app)
            {
                // Let Umbraco do its thing
                base.Configuration(app);
    
                // Call the authentication configration process (located in App_Start/Startup.Auth.cs)
                ConfigureAuth(app);
    
                // Hook up Auth0 controller
                RouteTable.Routes.MapRoute(
                    "Auth0Account",
                    "Auth0Account/{action}",
                    new
                    {
                        controller = "Auth0Account"
                    }
                );
            }
    
            private void ConfigureAuth(IAppBuilder app)
            {
                // Enable the application to use a cookie to store information for the signed in user
                app.UseCookieAuthentication(new CookieAuthenticationOptions
                {
                    AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
                    LoginPath = new PathString("/Member/Login") // Use whatever page has your login macro lives on
                });
    
                // Use a cookie to temporarily store information about a user logging in with a third party login provider
                app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
    
                app.UseAuth0Authentication(
                    clientId: ConfigurationManager.AppSettings["auth0:ClientId"],
                    clientSecret: ConfigurationManager.AppSettings["auth0:ClientSecret"],
                    domain: ConfigurationManager.AppSettings["auth0:Domain"]);
            }
        }
    }
    

    您会注意到我们连接了 Auth0 NuGet 包添加的 Auth0AccountController。如果我们不这样做,一旦 Auth0 在验证后将用户返回到我们的站点,我们就会得到 404。

    更改 web.config 中的 owin 启动以使用我们的新启动类:

    <add key="owin:appStartup" value="MyStartup" />
    

    2) 将 ~/signin-auth0 添加到 umbracoReservedPaths

    我们不希望 Umbraco CMS 处理 Auth0 使用的 ~/signin-auth0,因此我们更新 umbracoReservedPaths appSetting 以告诉它忽略它:

    <add key="umbracoReservedPaths" value="~/umbraco,~/install/,~/signin-auth0" />
    

    3) 修改 Auth0AccountController

    您需要修改 Auth0AccountController 以使用对您的 Umbraco 设置和您已配置/创建的页面友好的重定向。如果您不这样做,您将在用户通过身份验证后开始看到“路由表中的路由与提供的值不匹配”错误。您可能希望从 Umbraco.Web.Mvc.SurfaceControllerUmbraco.Web.Mvc.RenderMvcController 继承而不是标准 Controller,以便向您的代码公开 Umbraco 友好的属性和方法。

    然后,您可以在 Auth0AccountController 中连接您需要的任何代码,以自动为新用户创建新成员、为现有用户自动登录成员等。或者,如果您愿意,您可以简单地绕过 Umbraco 成员的使用并处理经过身份验证的用户以不同的方式。

    【讨论】:

      猜你喜欢
      • 2011-07-06
      • 2015-08-01
      • 2010-10-24
      • 1970-01-01
      • 2019-07-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-30
      相关资源
      最近更新 更多