I have integrated Auth0 as an Owin identity provider so that in the future I can manage website users via the Auth0 platform. After registration the user should be redirected to a form asking for some additional data (postal address, etc.), then a Sitecore user account should be created.
For the implementation I followed this project: Auth0 in Sitecore. I use the same processor class (Auth0IdentityProviderProcessor.cs), the same provider configuration (Auth0InSitecore.config) and also enabled federated authentication.
<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:role="http://www.sitecore.net/xmlconfig/role/" xmlns:set="http://www.sitecore.net/xmlconfig/set/">
<sitecore role:require="Standalone or ContentDelivery or ContentManagement">
<settings>
<setting name="Owin.Authentication.Enabled" value="true" />
<setting name="FederatedAuthentication.Enabled" value="true" />
</settings>
<services>
<register serviceType="Sitecore.Abstractions.BaseAuthenticationManager, Sitecore.Kernel"
implementationType="Sitecore.Owin.Authentication.Security.AuthenticationManager, Sitecore.Owin.Authentication"
lifetime="Singleton" />
<register serviceType="Sitecore.Abstractions.BaseTicketManager, Sitecore.Kernel"
implementationType="Sitecore.Owin.Authentication.Security.TicketManager, Sitecore.Owin.Authentication"
lifetime="Singleton" />
<register serviceType="Sitecore.Abstractions.BasePreviewManager, Sitecore.Kernel"
implementationType="Sitecore.Owin.Authentication.Publishing.PreviewManager, Sitecore.Owin.Authentication"
lifetime="Singleton" />
</services>
<sites>
<site name="shell" set:loginPage="/sitecore/login" role:require="Standalone or ContentManagement" />
<site name="admin" set:loginPage="/sitecore/admin/login.aspx" />
</sites>
</sitecore>
</configuration>
Of course I added the Auth0 credentials (client ID, redirect URI, etc.) and set the URLs (callback URL, logout URL) in my Auth0 development account. The callback URL would be https://mypage.de/Web/Account/LoginWithRole (I test on my local computer with an IIS site).
My account controller looks like this:
using System.Security.Claims;
using System.Web;
using System.Web.Mvc;
using Microsoft.Owin.Security;
using Microsoft.Owin.Security.Cookies;
using Sitecore;
using Sitecore.Links;
using Sitecore.Security.Authentication;
namespace Controllers
{
public class AccountController : AreaController
{
public ActionResult Login()
{
HttpContext
.GetOwinContext()
.Authentication
.Challenge(new AuthenticationProperties
{
RedirectUri = Url.Action("LoginWithRole", "Account", new { area = "" })
},
"Auth0"
);
return new HttpUnauthorizedResult();
}
public ActionResult LoginWithRole()
{
var claimsIdentity = User.Identity as ClaimsIdentity;
var eMail = claimsIdentity?.FindFirst(c => c.Type == ClaimTypes.Email)?.Value;
return Content(""); // only for testing
}
}
}
So, the login action method works as expected. Auth0 opens and I can register a new user via login form. When I go into SecurityTokenReceived handler (Auth0 processor) with debugger I can also see my data (name, access token, etc.). But the data is not accessible in LoginWithRole action method. I would like to get the Auth0 claims from User.Identity.
In the network panel of the browser developer console, I could see that Auth0 makes a POST request to https://mypage.de/Web/Account/LoginWithRole, but the program flow does not go into the method (I checked this while debugging). Instead, another redirect is made to the same URL using the GET method. Then the program flow goes into the method body, but the User.Identity property points to a Sitecore user object.
When I add attributes like Authorize or HttpPost to my action method, I get into a redirect loop (Login > LoginWithRole as POST > Login > etc.). I do not know why.
Maybe someone has had the same (or similar) problem before and knows how to solve it? Any suggestion would be really helpful.
Thanks in advance.