Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid exception: An item with the same key has already been added. Key: MicrosoftIdentityError #1382

Merged
merged 1 commit into from
Aug 12, 2021

Conversation

Shazwazza
Copy link
Contributor

@Shazwazza Shazwazza commented Aug 12, 2021

This exception occurs sometimes when there are errors during login. It's not always the case but perhaps something to do with Temp Data not being cleared during debugging/testing and restarting. In any case, it's easily solved by just setting the temp data value instead of adding it.

Update: I've replaced the ILoginErrorAccessor with my own just see how this is caused and it's not due to Temp Data not being cleared. It ends up being called twice if you accidentally redirect to the same oicd handler address that is handling it - then the same key is added twice. Obviously not common and shouldn't actually occur (oops!) but still can be avoided by not using "Set"

Stack trace

ArgumentException: An item with the same key has already been added. Key: MicrosoftIdentityError
System.Collections.Generic.Dictionary<TKey, TValue>.TryInsert(TKey key, TValue value, InsertionBehavior behavior)
System.Collections.Generic.Dictionary<TKey, TValue>.Add(TKey key, TValue value)
Microsoft.AspNetCore.Mvc.ViewFeatures.TempDataDictionary.Add(string key, object value)
Microsoft.Identity.Web.TempDataLoginErrorAccessor.SetMessage(HttpContext context, string message)
Microsoft.Identity.Web.AzureADB2COpenIDConnectEventHandlers.OnRemoteFailure(RemoteFailureContext context)
Microsoft.Identity.Web.MicrosoftIdentityWebAppAuthenticationBuilderExtensions+<>c__DisplayClass5_2+<<AddMicrosoftIdentityWebAppInternal>b__5>d.MoveNext()

…y: MicrosoftIdentityError

This exception occurs sometimes when there are errors during login. It's not always the case but perhaps something to do with Temp Data not being cleared during debugging/testing and restarting. In any case, it's easily solved by just setting the temp data value instead of adding it.

Stack trace

```
ArgumentException: An item with the same key has already been added. Key: MicrosoftIdentityError
System.Collections.Generic.Dictionary<TKey, TValue>.TryInsert(TKey key, TValue value, InsertionBehavior behavior)
System.Collections.Generic.Dictionary<TKey, TValue>.Add(TKey key, TValue value)
Microsoft.AspNetCore.Mvc.ViewFeatures.TempDataDictionary.Add(string key, object value)
Microsoft.Identity.Web.TempDataLoginErrorAccessor.SetMessage(HttpContext context, string message)
Microsoft.Identity.Web.AzureADB2COpenIDConnectEventHandlers.OnRemoteFailure(RemoteFailureContext context)
Microsoft.Identity.Web.MicrosoftIdentityWebAppAuthenticationBuilderExtensions+<>c__DisplayClass5_2+<<AddMicrosoftIdentityWebAppInternal>b__5>d.MoveNext()
```
Copy link
Collaborator

@jennyf19 jennyf19 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great. thanks @Shazwazza

@jennyf19 jennyf19 added this to the 1.16 milestone Aug 12, 2021
@jennyf19 jennyf19 assigned sachku and Shazwazza and unassigned sachku Aug 12, 2021
@jennyf19 jennyf19 merged commit d1b50c2 into AzureAD:master Aug 12, 2021
@Shazwazza Shazwazza deleted the patch-1 branch August 13, 2021 18:44
@jennyf19
Copy link
Collaborator

Included in 1.16 release

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants