使用用户名/密码连接到Azure AD [英] Connect to Azure AD using username/password

查看:73
本文介绍了使用用户名/密码连接到Azure AD的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Azure Api REST,可以通过输入用户名/密码来访问.我如何从C#中访问此API?

经过一番搜索,我发现有关使用 AuthenticationContext ..的方法对我来说不起作用Authenticationcontext .AcquireToken(resource,clientId,credential);

我在哪里可以获取'资源'参数和 ClientID .

非常感谢

解决方案

我的大部分工作都基于

客户端ID 是从此应用中获取的.

这是我当前用于获取可与Management SDK一起使用的令牌的代码:

  string userName ="yourUserName";字符串密码="yourPassword";字符串directoryName ="yourDirectory.onmicrosoft.com";字符串clientId ="{通过在Active Directory中创建应用程序而获得的ClientId}";var certificate = new UserPasswordCredential(string.Format("{0} @ {1}",userName,directoryName),密码);var authenticationContext = new AuthenticationContext("https://login.windows.net/" + directoryName);var result = await authenticationContext.AcquireTokenAsync("https://management.core.windows.net/",clientID,凭据);var jwtToken = result.AccessToken;//示例访问Azure Cdn管理API字符串subscriptionId ="xxxx-xxxxxx-xxxx-xxxxxxx";使用(var cdn = new CdnManagementClient(new TokenCredentials(jwtToken)){SubscriptionId = subscriptionId}){//做一点事...} 

您的目录名称可以在域名上的"Azure门户">"Azure AD"部分中获得.

I have an Azure Api REST which can be reach by entering a username/password . how can i get access to this API from C# ?

after a little search , i found somthing about using AuthenticationContext .. but it couldn't work for me Authenticationcontext .AcquireToken(resource, clientId, credential);

where can i get the 'resource' parameter and the ClientID .

Thanks a lot

解决方案

I based most of my work on this post that laid some groundwork.

You need to create a Native Application in your Azure AD first and add the Windows Azure Service Management API permission.

The ClientID is obtained from this App.

This is the code I'm currently using to obtain a Token that can be used with the Management SDK:

string userName = "yourUserName";
string password = "yourPassword";
string directoryName = "yourDirectory.onmicrosoft.com";
string clientId = "{ClientId obtained by creating an App in the Active Directory}";
var credentials= new UserPasswordCredential(string.Format("{0}@{1}", userName, directoryName), password);
var authenticationContext = new AuthenticationContext("https://login.windows.net/" + directoryName);
var result = await authenticationContext.AcquireTokenAsync("https://management.core.windows.net/", clientID, credentials);
var jwtToken = result.AccessToken;
//Example accesing Azure Cdn Management API
string subscriptionId = "xxxx-xxxxxx-xxxx-xxxxxxx";
using (var cdn = new CdnManagementClient(new TokenCredentials(jwtToken)) { SubscriptionId = subscriptionId })
{
    //do something...
}

Your directory name can be obtained in the Azure Portal > Azure AD section, on Domain names.

这篇关于使用用户名/密码连接到Azure AD的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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