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

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

问题描述

在ADAL.Net 3.x中,UserPasswordCredential在2.x的UserCredential之上引入.但是,在同一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仅使用一个参数用户名,因此如何输入用户密码并在.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天全站免登陆