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

标签 c# authentication umbraco umbraco7 auth0

我想集成 Auth0使用 Umbraco 7 对成员进行身份验证(成员是公共(public)网站的用户而不是后端 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"]);
        }
    }
}

您会注意到我们连接了由 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 设置和您配置/创建的页面友好的重定向。如果您不这样做,您将在用户通过身份验证后开始看到“路由表中没有路由与提供的值匹配”错误。为了向你的代码。

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

关于c# - 将 Auth0 与 Umbraco 7 集成以进行成员身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36050888/

相关文章:

mongodb - 使用 mongodb v2.4.8 创建具有 "dbAdminAnyDatabse"角色的管理员

url-rewriting - Umbraco 7 - 替代 URL

asp.net - 需要带有 aspx 扩展名的 umbraco 网站的 URL

asp.net - 从 umbraco 搜索中排除节点

javascript - 如何在 Web 浏览器中禁用网站 URL?

c# - 每次运行后清除与 tracelistener 关联的日志?

c# - 删除 Silverlight TextBox 鼠标悬停边框的最简单方法是什么?

c# - 数组作为函数参数 c 到 c# 的混淆

node.js - 如何在 node.js 中实现登录身份验证

php - 当用户注册时,我在 Laravel 5.1 中的哪里添加事件