AuthenticationContext.AcquireTokenAsync是C#的新手

ste*_*son 5 c# azure

我是Azure新手,希望能够以编程方式从Azure获取令牌.无论我做什么,它似乎都失败了 - 有没有人有一个有效的例子?多谢你们

我打电话GetAToken().Wait();.

方法是:

public async Task<string> GetAToken()
{
    // authentication parameters
    string clientID = "*********";
    string username = "<azure login>";
    string password = "<azure login password>";
    string directoryName = "<AD Domain name>";

    ClientCredential cc = new ClientCredential(clientID, password);
    var authenticationContext = new AuthenticationContext("https://login.windows.net/" + directoryName);

    AuthenticationResult result = await authenticationContext.AcquireTokenAsync("https://management.core.windows.net/", cc);

    if (result == null)
    {
        throw new InvalidOperationException("Failed to obtain the JWT token");
    }

    string token = result.AccessToken;

    return token;
}
Run Code Online (Sandbox Code Playgroud)

Bre*_*ate 8

所以不确定你是否在Android,iOS或Xamarin.Forms上这样做.下面是我将如何使用ADAL和Azure进行身份验证(代码正在我的工作中):

在Android上:

public async Task<AuthenticationResult> Authenticate(Activity context, string authority, string resource, string clientId, string returnUri)
{
    var authContext = new AuthenticationContext(authority);
    if (authContext.TokenCache.ReadItems().Any())
        authContext = new AuthenticationContext(authContext.TokenCache.ReadItems().First().Authority);

    var uri = new Uri(returnUri);
    var platformParams = new PlatformParameters(context);
    try
    {
        var authResult = await authContext.AcquireTokenAsync(resource, clientId, uri, platformParams);
        return authResult;
    }
    catch (AdalException e)
    {
        return null;
    }
}
Run Code Online (Sandbox Code Playgroud)

在iOS上:

public async Task<AuthenticationResult> Authenticate(UIViewController controller, string authority, string resource, string clientId, string returnUri)
    {
        var authContext = new AuthenticationContext(authority);
        if (authContext.TokenCache.ReadItems().Any())
            authContext = new AuthenticationContext(authContext.TokenCache.ReadItems().First().Authority);

        var controller = UIApplication.SharedApplication.KeyWindow.RootViewController;
        var uri = new Uri(returnUri);
        var platformParams = new PlatformParameters(controller);

        try
        {
            var authResult = await authContext.AcquireTokenAsync(resource, clientId, uri, platformParams);
            return authResult;
        }
        catch (AdalException e)
        {
            return null;
        }
    }
Run Code Online (Sandbox Code Playgroud)

UWP上:

public async Task<AuthenticationResult> Authenticate(string authority, string resource, string clientId, string returnUri)
{
    var authContext = new AuthenticationContext(authority);
    if (authContext.TokenCache.ReadItems().Any())
        authContext = new AuthenticationContext(authContext.TokenCache.ReadItems().First().Authority);

    var uri = new Uri(returnUri);
    var platformParams = new PlatformParameters(PromptBehavior.Auto);
    try
    {
        var authResult = await authContext.AcquireTokenAsync(resource, clientId, uri, platformParams);
        return authResult;
    }
    catch (AdalException e)
    {
        return null;
    }
}
Run Code Online (Sandbox Code Playgroud)

我传入上述方法的变量:

string authority = "https://login.windows.net/common";
string ResourceID = "Backend ClientId";//Backend (web app)
string clientId = "Native App ClientId";//native app
string returnUri = "https://{My Azure Site}.azurewebsites.net/.auth/login/done";
Run Code Online (Sandbox Code Playgroud)

如果你想在Xamarin.Forms中这样做,下面是我的GitHub解决方案的链接,我已经通过这些方法公开了这些方法DependencyService.

我希望这有帮助!如果您的响应中出现任何错误,请检查以确保您在Azure中正确设置了权限.我是这样做的.另一个很好的资源是Adrian Hall的Xamarin/Azure书

编辑:添加了UWP的东西