我尝试使用AAD对我的客户端进行身份验证,并使用Windows服务自动执行此操作。在AAD .NET SDK中,有两种方法,paginate_path: "/Blog/page:num/"
和AcquireTokenAsync
,但是我不能使用这些方法中的任何一种,await调用将永远保留,没有响应,并且当我做这样的事情时:
AcquireToken
该对象返回状态result = authContext.AcquireTokenAsync(resourceHostUri, clientId, new UserCredential(hardcodedUsername, hardcodedPassword)).Result;
& Waiting for Activation
..
现在,无论如何使用硬编码的用户名和密码获取令牌?
我的完整代码:
Code 31
我试图访问Azure API。
更新1:
当我尝试 string hardcodedUsername = "username";
string hardcodedPassword = "password";
string tenant = "tenantId@onmicrosoft.com";
string clientId = "clientId";
string resourceHostUri = "https://management.azure.com/";
string aadInstance = "https://login.microsoftonline.com/{0}";
string authority = String.Format(CultureInfo.InvariantCulture, aadInstance, tenant);
authContext = new AuthenticationContext(authority);
AuthenticationResult result = null;
try
{
result = authContext.AcquireTokenAsync(resourceHostUri, clientId, new UserCredential(hardcodedUsername, hardcodedPassword)).Result;
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}
return result;
通话时,我在输出中得到了这个,我认为这可能会有所帮助:
Microsoft.IdentityModel.Clients.ActiveDirectory TokenCache:查找令牌的缓存... Microsoft.IdentityModel.Clients.ActiveDirectory TokenCache:在缓存中找不到匹配的标记 Microsoft.IdentityModel.Clients.ActiveDirectory d__0:将用户域发现请求发送到' https://login.microsoftonline.com/common/UserRealm/ 用户名?api-version = 1.0' Microsoft.IdentityModel.Clients.ActiveDirectory d__4:用户使用哈希' ***'被检测为联邦'
答案 0 :(得分:0)
尝试以下链接代码
https://msdn.microsoft.com/en-in/library/partnercenter/dn974935.aspx
how to get access token after windows azure active directory authentication
How to get current token from Azure ActiveDirectory application
def f(x:Any)=x-5
<console>:54: error: value - is not a member of Any
def f(x:Any)=x-5
^
答案 1 :(得分:0)
请尝试以下方法:
static void Main(string[] args)
{
Task<AuthenticationResult> t = getAccessToken();
t.Wait();
var result = t.Result;
Console.WriteLine(result.AccessToken);
Console.WriteLine("Please any key to terminate the program");
Console.ReadKey();
}
public static async Task<AuthenticationResult> getAccessToken()
{
string hardcodedUsername = "username";
string hardcodedPassword = "password";
string tenant = "tenant.onmicrosoft.com";
string clientId = "clientId";
string resourceHostUri = "https://management.azure.com/";
string aadInstance = "https://login.microsoftonline.com/{0}";
string authority = String.Format(CultureInfo.InvariantCulture, aadInstance, tenant);
var authContext = new AuthenticationContext(authority);
AuthenticationResult result = null;
try
{
result = await authContext.AcquireTokenAsync(resourceHostUri, clientId, new UserCredential(hardcodedUsername, hardcodedPassword));
}
catch (Exception ex)
{
Console.WriteLine(ex.StackTrace);
System.Diagnostics.Debug.WriteLine(ex.Message);
}
return result;
}
我所做的是getAccessToken()
方法async
,其内部代码是在您拨打authContext.AcquireTokenAsync
时等待获取令牌。