Gav*_*vin 3 c# authentication umbraco umbraco7 auth0
我想将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"]);
}
}
}
Run Code Online (Sandbox Code Playgroud)
你会注意到我们连接了Auth0AccountControllerAuth0 NuGet包.如果我们不这样做,一旦Auth0在验证后将用户返回到我们的网站,我们将获得404.
更改web.config中的owin启动以使用我们的新启动类:
<add key="owin:appStartup" value="MyStartup" />
Run Code Online (Sandbox Code Playgroud)
2)将〜/ signin-auth0添加到umbracoReservedPaths
我们不希望Umbraco CMS处理Auth0使用的〜/ signin-auth0,所以我们更新umbracoReservedPaths appSetting告诉它忽略它:
<add key="umbracoReservedPaths" value="~/umbraco,~/install/,~/signin-auth0" />
Run Code Online (Sandbox Code Playgroud)
3)修改Auth0AccountController
您需要修改Auth0AccountController使用对您的Umbraco设置和您已配置/创建的页面友好的重定向.如果您不这样做,您将在用户进行身份验证后开始看到"路由表中的路由没有匹配提供的值"错误.您可能希望继承Umbraco.Web.Mvc.SurfaceController或Umbraco.Web.Mvc.RenderMvcController替代标准Controller,以便向代码公开Umbraco友好属性和方法.
然后,您可以在Auth0AccountController中连接所需的任何代码,以便为新用户自动创建新成员,自动为现有用户登录成员等.或者,如果您愿意,可以简单地绕过使用Umbraco成员并以不同方式处理经过身份验证的用户方式.