Skip to content

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

Merged
jennyf19 merged 1 commit into
AzureAD:masterfrom
Shazwazza:patch-1
Aug 12, 2021
Merged

jennyf19 merged 1 commit into
AzureAD:masterfrom
Shazwazza:patch-1

Conversation

@Shazwazza

@Shazwazza Shannon Deminick (Shazwazza) commented Aug 12, 2021 •

Copy link
Copy Markdown
Contributor

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()
```

@jennyf19 jennyf19 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

@jennyf19 jennyf19 added this to the 1.16 milestone Aug 12, 2021
@jennyf19
jennyf19 merged commit d1b50c2 into AzureAD:master Aug 12, 2021
@jennyf19

Copy link
Copy Markdown
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