繁体   English   中英

将Auth0与Umbraco 7集成以进行成员身份验证

[英]Integrate Auth0 with Umbraco 7 for member authentication

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

整合两者需要哪些步骤?

为了一个干净的解决方案,我创建了一个空的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"]);
        }
    }
}

你会发现,我们挂钩的Auth0AccountController由Auth0 NuGet包添加。 如果我们不这样做,一旦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成员并以不同方式处理经过身份验证的用户方式。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM