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

[Help] I am using the method AcquireTokenByUsernamePassword to get the token, but I get the following error. I haven't configured it properly. thank you #3883

Closed
kou-h opened this issue Dec 27, 2022 · 5 comments · Fixed by #3937

Comments

@kou-h
Copy link

kou-h commented Dec 27, 2022

I am using the method AcquireTokenByUsernamePassword to get the token, but I get the following error. I haven't configured it properly. thank you

Microsoft.Identity.Client:Version="4.49.1"

code:
public readonly static string[] ApiScopes = { "openid", "profile", "offline_access", "User.ManageIdentities.All", "User.Read.All","User.ReadBasic.All","User.Invite.All","User.ReadWrite.All","IdentityProvider.Read.All" };

app = PublicClientApplicationBuilder.Create(ClientId)
.WithB2CAuthority(AuthoritySignUpSignIn)
.WithRedirectUri("http://localhost:8080/home")
.WithExtraQueryParameters(new Dictionary<string, string>() { { "nonce", Guid.NewGuid().ToString() } })
.WithLogging(Log, LogLevel.Info, false)
.Build();

authResult = await app.AcquireTokenByUsernamePassword(App.ApiScopes, "UserName", "UserPassword").ExecuteAsync();

Error Message:
Error Acquiring Token:
Microsoft.Identity.Json.JsonReaderException: Unexpected character encountered while parsing value: <. Path '', line 0, position 0.
at Microsoft.Identity.Json.JsonTextReader.ParseValue()
at Microsoft.Identity.Json.JsonReader.ReadForType(JsonContract contract, Boolean hasConverter)
at Microsoft.Identity.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
at Microsoft.Identity.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
at Microsoft.Identity.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)
at Microsoft.Identity.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings)
at Microsoft.Identity.Client.Utils.JsonHelper.DeserializeFromJson[T](String json)
at Microsoft.Identity.Client.OAuth2.OAuth2Client.CreateResponse[T](HttpResponse response, RequestContext requestContext)
at Microsoft.Identity.Client.OAuth2.OAuth2Client.ExecuteRequestAsync[T](Uri endPoint, HttpMethod method, RequestContext requestContext, Boolean expectErrorsOn200OK, Boolean addCommonHeaders, Func2 onBeforePostRequestData) at Microsoft.Identity.Client.OAuth2.OAuth2Client.GetTokenAsync(Uri endPoint, RequestContext requestContext, Boolean addCommonHeaders, Func2 onBeforePostRequestHandler)
at Microsoft.Identity.Client.OAuth2.TokenClient.SendHttpAndClearTelemetryAsync(String tokenEndpoint, ILoggerAdapter logger)
at Microsoft.Identity.Client.OAuth2.TokenClient.SendTokenRequestAsync(IDictionary`2 additionalBodyParameters, String scopeOverride, String tokenEndpointOverride, CancellationToken cancellationToken)
at Microsoft.Identity.Client.Internal.Requests.UsernamePasswordRequest.GetTokenResponseAsync(CancellationToken cancellationToken)
at Microsoft.Identity.Client.Internal.Requests.UsernamePasswordRequest.ExecuteAsync(CancellationToken cancellationToken)
at Microsoft.Identity.Client.Internal.Requests.RequestBase.RunAsync(CancellationToken cancellationToken)
at Microsoft.Identity.Client.ApiConfig.Executors.PublicClientExecutor.ExecuteAsync(AcquireTokenCommonParameters commonParameters, AcquireTokenByUsernamePasswordParameters usernamePasswordParameters, CancellationToken cancellationToken)

@pmaytak
Copy link
Contributor

pmaytak commented Dec 28, 2022

@kou-h
This parsing issue sometimes happens when the application is not set up correctly in Azure; and Azure AD returns an unusual error response.

Verify that this B2C app is correctly set up in Azure portal. See this wiki and this wiki. You may have to create a new flow (other than SignUpSignIn) for Username/Password. Also note the limitations and potential error causes - password is wrong or expired, non-local B2C account is used.

Also our usual disclaimer - Username/Password flow is not recommended because it's not secure.

@bgavrilMS
Copy link
Member

Still, one can argue that MSAL should throw a friendlier exception and not a JSON parsing error. I would recommend treating this as a bug.

@kou-h - a few thoughts:

  1. You can't ask for Graph scopes with a B2C user. Graph only works with Azure AD authority
  2. Username / Password is insecure because apps should not handle passwowrds. But apart from that, the flow will not work with MFA. So please use AcquireTokenInteractive instead.
  3. Can you capture a network trace and send it over? Use bogavril at microsoft.com

@bgavrilMS bgavrilMS self-assigned this Jan 23, 2023
@bgavrilMS bgavrilMS removed their assignment Jan 31, 2023
@bgavrilMS bgavrilMS added this to the 4.50.0 milestone Jan 31, 2023
@SameerK-MSFT
Copy link
Contributor

@kou-h what version of .net are you using?

@kou-h
Copy link
Author

kou-h commented Feb 1, 2023

@SameerK-MSFT
Is it the version of. net core? If yes, I use. net core 6.0

@bgavrilMS
Copy link
Member

@SameerK-MSFT - not sure why this matters? It's just about throwing a better exception if folks attempt ROPC with B2C authority and graph scope.

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

Successfully merging a pull request may close this issue.

5 participants