Multiple Logins using ExternalLogin (via Auth0)
# help-with-umbraco
c
Hi, I have setup an U13 website and Im using Auth0 using the docs, and this example code: https://github.com/jbreuer/Umbraco-OpenIdConnect-Example, and it's mostly working great, I'm using this for Members only. I initially set it up with just one provider (Google) but I need to support multiple (over 10). The free testing account with Auth0 allows you to setup 2, so I've connected Google and Microsoft. I have autolinking setup so it creates a member in the Umbraco Backoffice, this is great. So the scenario: A user logins in using their Google account and it creates their account with the name: user@website.com they get logged in, and everything is good. They log out and try to login with Microsoft account (which also uses user@website.com) everything goes through the right steps but when I land back at my umbraco instance I am present with this error:
Cannot insert duplicate key row in object 'dbo.umbracoExternalLogin' with unique index 'IX_umbracoExternalLogin_LoginProvider'. The duplicate key value is (UmbracoMembers.OpenIdConnect, *{{insert memeber's guid here}}*). The statement has been terminated.
I need the system to create different members for each social login, does anyone have any advice on how to do this, a link to a blog about it, or a link to documentation? Thanks in advanced.
d
It sounds like you have connected multiple providers with the same name. You may have some code in your registration setup, similar to this (this example is from the backoffice):
Copy code
csharp
authBuilder.SchemeForBackOffice(MyAuthenticationOptions.AuthenticationScheme)
If you have multiple login providers, you need to ensure that the authentication scheme that you provide here is unique for each provider
oh, I didn't notice the part that you need different members for each social login. As far as I know, all your members need a unique email adres.
You may be able to use email aliases to overcome this, but I don't know to what extent this is feasible
c
Thanks for the response, so I'm using one provider which is Auth0, as Im sure you're aware they're are a middle man for these OpenId providers. If I cant have different members for each provider then thats fine, I haven't really got a hard and fast requirement to have different members per provider at this point. Attached is the code Im using at the moment, Im not sure what I need to change and where at achieve the outcome I need. https://cdn.discordapp.com/attachments/1256247762295586929/1256266803739496459/message.txt?ex=66802522&is=667ed3a2&hm=c6867d6b27e3573f36461b7eb0460b4b108181f90015a68147fe0c5154003854&
This feels a bit hacky, but I managed to get different providerId's for the same member by doing this:
Copy code
OnAutoLinking = (autoLinkUser, externalLogin) =>
{
    // You can customize the user before it's linked.
    // i.e. Modify the user's groups based on the Claims returned
    // in the externalLogin info
    
    var providerSuffix = externalLogin.ProviderKey.Split('|')[0];
    externalLogin.LoginProvider += $".{providerSuffix}";
}
This feels horrible but works a treat, and feels like its a future me problem to fix if it becomes an issue 😄
d
Ai, well good that you found something that works
c
@D_Inventor do you know if I can enable the delivery api and have it protected by the same auth system, so a windows app can using auth0 to authentiate and then call the delivery api with the tokens from auth0?
d
I have no experience with the delivery api, I couldn't you
c
no worries thanks for the advice, after I hit enter I think I found out how to do it via the docs: https://docs.umbraco.com/umbraco-cms/reference/content-delivery-api/protected-content-in-the-delivery-api
d
@Cynical Developer I am trying to get Auth0 to work for protected media files and using this code for interupt the context load: await context.ChallengeAsync("UmbracoMembers.Auth0", new AuthenticationProperties { RedirectUri = $"{context.Request.Scheme}://{context.Request.Host}{returnUrl}" }); return; But the challange and returnUrl will fail after sign in form on auth0. Have you wrote any code like this? (I am using the code from your project for the login and that works great but not my challenge 😦
151 Views