将 Auth0 与 Umbraco 7 集成以进行成员身份验证
Integrate Auth0 with Umbraco 7 for member authentication
我想将 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 returns 用户通过身份验证后访问我们的网站时获得 404。
更改 web.config 中的 owin 启动以使用我们的新启动 class:
<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 设置和您配置/创建的页面友好的重定向。如果您不这样做,您将在用户通过身份验证后开始看到 "No route in the route table matches the supplied values" 错误。您可能希望继承 Umbraco.Web.Mvc.SurfaceController
或 Umbraco.Web.Mvc.RenderMvcController
而不是标准 Controller,以便将 Umbraco 友好的属性和方法暴露给您的代码。
然后您可以在 Auth0AccountController 中连接您需要的任何代码,以自动为新用户创建新会员,为现有用户自动登录会员等。或者如果您愿意,您可以简单地绕过 Umbraco 会员的使用并处理以不同方式验证用户。
我想将 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 returns 用户通过身份验证后访问我们的网站时获得 404。
更改 web.config 中的 owin 启动以使用我们的新启动 class:
<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 设置和您配置/创建的页面友好的重定向。如果您不这样做,您将在用户通过身份验证后开始看到 "No route in the route table matches the supplied values" 错误。您可能希望继承 Umbraco.Web.Mvc.SurfaceController
或 Umbraco.Web.Mvc.RenderMvcController
而不是标准 Controller,以便将 Umbraco 友好的属性和方法暴露给您的代码。
然后您可以在 Auth0AccountController 中连接您需要的任何代码,以自动为新用户创建新会员,为现有用户自动登录会员等。或者如果您愿意,您可以简单地绕过 Umbraco 会员的使用并处理以不同方式验证用户。