ADAL .Net Core nuget 包不支持 UserPasswordCredential [英] ADAL .Net Core nuget package does not support UserPasswordCredential

查看:23
本文介绍了ADAL .Net Core nuget 包不支持 UserPasswordCredential的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 ADAL.Net 3.x 中,在 2.x 的 UserCredential 之上引入了 UserPasswordCredential.但是在同一个nuget包下的.Net Core中没有暴露同一个UserPasswordCredential?

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

UserCredential 类只有一个属性 UserName

UserCredential class has only one property UserName

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; }
    }
}

由于UserPasswordCredential在.NetCore中不可用,而UserCredential只有一个参数username,如何在.Net Core中输入用户密码并实现下面的代码?

Since UserPasswordCredential is not available in .NetCore and UserCredential takes only one parameter username, how to input the password of the user and implement below code in .Net Core?

authContext.AcquireTokenAsync(WebAPIResourceId, ClientId, userPasswordCredential);

我在 .Net Core 1.0 版本中专门使用 ADAL 3.13.4 版本

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

推荐答案

要使用资源所有者密码凭据授予流程来获取 Azure AD 的访问令牌,我们可以使用 HttpClient 直接调用 http 请求.这是一个示例供您参考:

To use the resource owner password credentials grant flow to get the access token for Azure AD, we can call the http request diectly using the HttpClient. Here is an example for your reference :

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

这篇关于ADAL .Net Core nuget 包不支持 UserPasswordCredential的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆