Closed marcuslindblom closed 5 years ago
Hey Marcus,
I'm out of the country at the moment, but I'll return in a week and have a look then.
The problem seems to be when I use ExternalLoginInfo
. This code throws an exception.
var info = await _signInManager.GetExternalLoginInfoAsync();
var email = info.Principal.FindFirstValue(ClaimTypes.Email);
var user = await _userManager.FindByEmailAsync(email);
await _userManager.AddLoginAsync(user, info);
await _session.SaveChangesAsync();
If I create a new UserLoginInfo
it seems to work like expected.
var info = await _signInManager.GetExternalLoginInfoAsync();
var email = info.Principal.FindFirstValue(ClaimTypes.Email);
var user = await _userManager.FindByEmailAsync(email);
await _userManager.AddLoginAsync(user, new UserLoginInfo(info.LoginProvider, info.ProviderKey, info.ProviderDisplayName));
await _session.SaveChangesAsync();
I'm back from my travels and I'm looking at this today. I'll let you know what I find.
Hi Marcus,
So it looks like folks on other databases hit a similar problem, where Newtonsoft.Json can't serialize claims.
The workaround shown in that thread is to use a customer serializer for claims, principals, and identities.
For Raven, I think the way that would work is like this:
docStore.Conventions.CustomizeJsonSerializer = (serializer) =>
{
serializer.Converters.Add(new JsonClaimConverter());
serializer.Converters.Add(new JsonClaimsPrincipalConverter());
serializer.Converters.Add(new JsonClaimsIdentityConverter());
};
And here are the implementations for those:
Can you give that a try and see if it works?
@JudahGabriel That seems to work. I guess this serialized data seems like the correct one?
"Logins": [
{
"$type": "Microsoft.AspNetCore.Identity.ExternalLoginInfo, Microsoft.AspNetCore.Identity",
"Principal": {
"Identities": [
{
"AuthenticationType": "Google",
"IsAuthenticated": true,
"Actor": null,
"BootstrapContext": null,
"Claims": [
{
"Type": "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier",
"Value": "112489450863147312520",
"ValueType": "http://www.w3.org/2001/XMLSchema#string",
"Issuer": "Google",
"OriginalIssuer": "Google"
},
{
"Type": "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name",
"Value": "Marcus Lindblom",
"ValueType": "http://www.w3.org/2001/XMLSchema#string",
"Issuer": "Google",
"OriginalIssuer": "Google"
},
{
"Type": "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/givenname",
"Value": "Marcus",
"ValueType": "http://www.w3.org/2001/XMLSchema#string",
"Issuer": "Google",
"OriginalIssuer": "Google"
},
{
"Type": "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/surname",
"Value": "Lindblom",
"ValueType": "http://www.w3.org/2001/XMLSchema#string",
"Issuer": "Google",
"OriginalIssuer": "Google"
},
{
"Type": "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress",
"Value": "m@xxx.com",
"ValueType": "http://www.w3.org/2001/XMLSchema#string",
"Issuer": "Google",
"OriginalIssuer": "Google"
}
],
"Label": null,
"Name": "Marcus Lindblom",
"NameClaimType": "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name",
"RoleClaimType": "http://schemas.microsoft.com/ws/2008/06/identity/claims/role"
}
]
},
"AuthenticationTokens": [],
"LoginProvider": "Google",
"ProviderKey": "xxx",
"ProviderDisplayName": "Google"
}
]
Looks good to me. Since this is a viable workaround, I'm going to close this issue.
Hi @marcuslindblom . How are you?
Have you tried to update this work around to RavenDB 5 client?
Looks like they changed the conventions override path. =/
@gabrielbarceloscn I have successfully upgraded my projects. I'll post some code when I get back home
@gabrielbarceloscn Change to code to the following and it all should work again 👍
options.BeforeInitializeDocStore = store =>
{
store.Conventions.Serialization = new NewtonsoftJsonSerializationConventions {
CustomizeJsonSerializer = (serializer) => {
serializer.Converters.Add(new JsonClaimConverter());
serializer.Converters.Add(new JsonClaimsPrincipalConverter());
serializer.Converters.Add(new JsonClaimsIdentityConverter());
}
};
I had the same issue and registering Claim converters as suggested above fixed the issue.
But I later found that I forgot to await the main thread that kicked off the serialisation. After adding back the await everything works as expected even without the custom claim converters
I'm using version 6.1 of RavenDB.Identity with dotnet core preview 8.
My config looks like this:
When I try to register a new account using Google I get a
System.PlatformNotSupportedException: This instance contains state that cannot be serialized and deserialized on this platform.
with the following stack trace.Maybe this has to do with the new
System.Text.Json
APIs? Or do you have any clue on whats has changed?