From 3922149ff5f1c2e10fe2ddca468b212c90ae6372 Mon Sep 17 00:00:00 2001 From: Shannon Deminick Date: Fri, 13 Aug 2021 04:51:39 +1000 Subject: [PATCH] Avoid exception: An item with the same key has already been added. Key: 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.TryInsert(TKey key, TValue value, InsertionBehavior behavior) System.Collections.Generic.Dictionary.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+<b__5>d.MoveNext() ``` --- src/Microsoft.Identity.Web/TempDataLoginErrorAccessor.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.Identity.Web/TempDataLoginErrorAccessor.cs b/src/Microsoft.Identity.Web/TempDataLoginErrorAccessor.cs index da5e6812f..48d10acee 100644 --- a/src/Microsoft.Identity.Web/TempDataLoginErrorAccessor.cs +++ b/src/Microsoft.Identity.Web/TempDataLoginErrorAccessor.cs @@ -59,7 +59,7 @@ public void SetMessage(HttpContext context, string? message) { _tempData = _factory.GetTempData(context); - _tempData.Add(Name, message); + _tempData[Name] = message; _tempData.Save(); } }