Skip to content

AuthenticationManager sample class failed to get an auth token  #6927

@liuf-99

Description

@liuf-99

I follow the directions here and use the AuthenticationManager sample class without any change except replacing the defaultAADAppId, but get a system.Collections.Generic.KeyNotFoundException exception at the code below (var tokenResult = JsonSerializer.Deserialize(result);). I also elclosed the call stack and detailed error info. FYI, I am a Microsoft employee, so I use my Microsoft email and https://microsoft-my.sharepoint-df.com/personal/myalias_microsoft_com as the username and siteUrl, respectively. I am trying to verify some CSOM calls for product development need and highly appreciate if you could help in the early convenience! I saw some previous posts that might be related, e.g., #6501, but this page was updated pretty recent on 12/21/2020. So I'd like to reach out to you first.

    private async Task<string> AcquireTokenAsync(Uri resourceUri, string username, string password)
    {
        string resource = $"{resourceUri.Scheme}://{resourceUri.DnsSafeHost}";

        var clientId = defaultAADAppId;
        var body = $"resource={resource}&client_id={clientId}&grant_type=password&username={HttpUtility.UrlEncode(username)}&password={HttpUtility.UrlEncode(password)}";
        using (var stringContent = new StringContent(body, Encoding.UTF8, "application/x-www-form-urlencoded"))
        {

            var result = await httpClient.PostAsync(tokenEndpoint, stringContent).ContinueWith((response) =>
            {
                return response.Result.Content.ReadAsStringAsync().Result;
            }).ConfigureAwait(false);

==> var tokenResult = JsonSerializer.Deserialize(result);
var token = tokenResult.GetProperty("access_token").GetString();
return token;
}
}

System.Collections.Generic.KeyNotFoundException: 'The given key was not present in the dictionary.'

ValueKind = Object : "{"error":"interaction_required","error_description":"AADSTS50076: Due to a configuration change made by your administrator, or because you moved to a new location, you must use multi-factor authentication to access '00000003-0000-0ff1-ce00-000000000000'.\r\nTrace ID: 5c113a27-fe5a-428b-bf84-0cf049e50300\r\nCorrelation ID: e9ca24c0-31ed-487e-bfa8-09897f9f2d96\r\nTimestamp: 2021-04-16 15:32:46Z","error_codes":[50076],"timestamp":"2021-04-16 15:32:46Z","trace_id":"5c113a27-fe5a-428b-bf84-0cf049e50300","correlation_id":"e9ca24c0-31ed-487e-bfa8-09897f9f2d96","error_uri":"https://login.microsoftonline.com/error?code=50076","suberror":"basic_action"}"

System.Text.Json.dll!System.Text.Json.JsonElement.GetProperty(string propertyName) Line 123	C#

CSOMTest.exe!CSOMTest.AuthenticationManager.AcquireTokenAsync(System.Uri resourceUri, string username, string password) Line 207 C#
[Resuming Async Method]
mscorlib.dll!System.Runtime.CompilerServices.AsyncMethodBuilderCore.MoveNextRunner.InvokeMoveNext(object stateMachine) Line 1090 C#
mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx) Line 980 C#
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx) Line 928 C#
mscorlib.dll!System.Runtime.CompilerServices.AsyncMethodBuilderCore.MoveNextRunner.Run() Line 1070 C#
mscorlib.dll!System.Runtime.CompilerServices.AsyncMethodBuilderCore.OutputAsyncCausalityEvents.AnonymousMethod__0() Line 977 C#
mscorlib.dll!System.Runtime.CompilerServices.AsyncMethodBuilderCore.ContinuationWrapper.Invoke() Line 1123 C#
mscorlib.dll!System.Runtime.CompilerServices.TaskAwaiter.OutputWaitEtwEvents.AnonymousMethod__0() Line 288 C#
mscorlib.dll!System.Runtime.CompilerServices.AsyncMethodBuilderCore.ContinuationWrapper.Invoke() Line 1123 C#
mscorlib.dll!System.Threading.Tasks.AwaitTaskContinuation.RunOrScheduleAction(System.Action action, bool allowInlining, ref System.Threading.Tasks.Task currentTask) Line 810 C#
mscorlib.dll!System.Threading.Tasks.Task.FinishContinuations() Line 3617 C#
mscorlib.dll!System.Threading.Tasks.Task.FinishStageThree() Line 2363 C#
mscorlib.dll!System.Threading.Tasks.Task.FinishStageTwo() Line 2336 C#
mscorlib.dll!System.Threading.Tasks.Task.Finish(bool bUserDelegateExecuted) Line 2253 C#
mscorlib.dll!System.Threading.Tasks.Task.ExecuteWithThreadLocal(ref System.Threading.Tasks.Task currentTaskSlot) Line 2830 C#
mscorlib.dll!System.Threading.Tasks.Task.ExecuteEntry(bool bPreventDoubleExecution) Line 2767 C#
mscorlib.dll!System.Threading.Tasks.Task.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem() Line 2704 C#
mscorlib.dll!System.Threading.ThreadPoolWorkQueue.Dispatch() Line 820 C#
mscorlib.dll!System.Threading._ThreadPoolWaitCallback.PerformWaitCallback() Line 1161 C#
[Async Call Stack]
[Async] CSOMTest.exe!CSOMTest.AuthenticationManager.EnsureAccessTokenAsync(System.Uri resourceUri, string userPrincipalName, string userPassword) Line 134 C#


Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:docs-commentCategory: Comment left on bottom of a docs page here: https://docs.microsoft.com/sharepoint/devtype:archive-old-issueIssues which are closed as tool old for active worktype:questionQuestion... if answered, will be tagged as such.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions