ADAL.NET Core nuget package does not support UserPasswordCredential

In ADAL.Net 3.x, the UserPasswordCredential is introduced on top of the UserCredential of 2.x. But the same UserPasswordCredential is not displayed in .Net Core under the same nuget package?

The UserCredential class has only one UserName property.

namespace Microsoft.IdentityModel.Clients.ActiveDirectory
{
    //
    // Summary:
    //     Credential used for integrated authentication on domain-joined machines.
    public class UserCredential
    {
        //
        // Summary:
        //     Constructor to create user credential. Using this constructor would imply integrated
        //     authentication with logged in user and it can only be used in domain joined scenarios.
        public UserCredential();
        //
        // Summary:
        //     Constructor to create credential with client id and secret
        //
        // Parameters:
        //   userName:
        //     Identifier of the user application requests token on behalf.
        public UserCredential(string userName);

        //
        // Summary:
        //     Gets identifier of the user.
        public string UserName { get; }
    }
}

Since UserPasswordCredential is not available at. NetCore and UserCredential accepts only one parameter username, how to enter user password and implement below code in .Net Core?

authContext.AcquireTokenAsync(WebAPIResourceId, ClientId, userPasswordCredential);

I am using ADAL version 3.13.4 specifically in .Net Core 1.0

+4
source share
2 answers

, UserPasswordCredential .NET Core, UserCredential . , ADAL v3 .NET Core.

+4

, , Azure AD, http HttpClient. :

HttpClient client = new HttpClient();
string tokenEndpoint = "https://login.microsoftonline.com/{tenantId}/oauth2/token";
var body = "resource={resourceUrl}&client_id={clientId}&grant_type=password&username={userName}&password={password}";
var stringContent = new StringContent(body, Encoding.UTF8, "application/x-www-form-urlencoded");

var result=await client.PostAsync(tokenEndpoint, stringContent).ContinueWith<string>((response) =>
{
    return response.Result.Content.ReadAsStringAsync().Result;
});

JObject jobject = JObject.Parse(result);

var token = jobject["access_token"].Value<string>();
+4

Source: https://habr.com/ru/post/1654110/


All Articles