简体   繁体   English

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

[英]Integrate Auth0 with Umbraco 7 for member authentication

I'd like to integrate Auth0 with Umbraco 7 for authentication of members (members are users of the public website rather than backend CMS users). 我想将Auth0与Umbraco 7集成以进行成员身份验证(成员是公共网站的用户,而不是后端CMS用户)。

What are the steps required to integrate the two? 整合两者需要哪些步骤?

For a clean solution I created an empty ASP.NET MVC project and added Umbraco using NuGet. 为了一个干净的解决方案,我创建了一个空的ASP.NET MVC项目,并使用NuGet添加了Umbraco。 I also pulled in Auth0 using NuGet. 我还使用NuGet引入了Auth0。

1) Override UmbracoDefaultOwinStartup 1)覆盖UmbracoDefaultOwinStartup

Add Startup.cs to the solution, inheriting from UmbracoDefaultOwinStartup so we can still let Umbraco do it's thing: 将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"]);
        }
    }
}

You'll notice that we hook up the Auth0AccountController added by the Auth0 NuGet package. 你会发现,我们挂钩的Auth0AccountController由Auth0 NuGet包添加。 If we don't do this we'll get 404's once Auth0 returns the user to our site after authenticating. 如果我们不这样做,一旦Auth0在验证后将用户返回我们的网站,我们将获得404。

Change the owin startup in web.config to use our new startup class: 更改web.config中的owin启动以使用我们的新启动类:

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

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

We don't want Umbraco CMS to handle the ~/signin-auth0 used by Auth0 so we update the umbracoReservedPaths appSetting to tell it to ignore it: 我们不希望Umbraco CMS处理Auth0使用的〜/ signin-auth0,所以我们更新umbracoReservedPaths appSetting告诉它忽略它:

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

3) Modify Auth0AccountController 3)修改Auth0AccountController

You'll need to modify the Auth0AccountController to use redirects that are friendly to your Umbraco setup and pages you've configured / created. 您需要修改Auth0AccountController以使用对您的Umbraco设置和您已配置/创建的页面友好的重定向。 If you don't do this you'll start seeing "No route in the route table matches the supplied values" errors after a user has authenticated. 如果您不这样做,您将在用户进行身份验证后开始看到“路由表中的路由没有匹配提供的值”错误。 You may want to inherit from Umbraco.Web.Mvc.SurfaceController or Umbraco.Web.Mvc.RenderMvcController instead of the standard Controller in order to expose Umbraco friendly properties and methods to your code. 您可能希望继承Umbraco.Web.Mvc.SurfaceControllerUmbraco.Web.Mvc.RenderMvcController而不是标准Controller,以便向代码公开Umbraco友好属性和方法。

You can then hook up any code you need in Auth0AccountController to automatically create new Members for new users, automatically login Members for existing users, etc. Or if you prefer you can simply bypass the use of Umbraco Members and handle the authenticated user in a different manner. 然后,您可以在Auth0AccountController中连接所需的任何代码,以便为新用户自动创建新成员,自动为现有用户登录成员等。或者,如果您愿意,可以简单地绕过使用Umbraco成员并以不同方式处理经过身份验证的用户方式。

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

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