使用ADAL进行身份验证

时间:2018-01-03 11:05:11

标签: azure azure-active-directory

我使用以下代码验证Azure试用帐户中的默认用户。

    static void Main(string[] args)
    {
        GetTokenAsync().Wait();
    }

    static async Task<string> GetTokenAsync()
    {
        string Tenant = "mytest.onmicrosoft.com";
        string Authority = "https://login.microsoftonline.com/" + Tenant;
        string GatewayLoginUrl = "https://login.microsoftonline.com/something/wsfed";
        string ClientId = "something";
        Uri RedirectUri = new Uri("http://something");

        AuthenticationContext context = new AuthenticationContext(Authority);
        PlatformParameters platformParams = new PlatformParameters(PromptBehavior.Auto, null);
        AuthenticationResult result = await context.AcquireTokenAsync(GatewayLoginUrl, ClientId, RedirectUri, platformParams);

        return result.ToString();
    }

我想知道从哪里获取这些值:

  • 租客
  • 当局
  • GatewayLoginUrl
  • 客户端Id
  • RedirectUri

这么多代码是否足以使用AD进行用户身份验证?

1 个答案:

答案 0 :(得分:2)

使用Azure Active Directory保护应用程序时有几个方案(参见here):

  

这些是Azure AD支持的五种主要应用程序方案:

     
      
  1. Web浏览器到Web应用程序:用户需要登录由Azure AD保护的Web应用程序。
  2.   
  3. 单页应用程序(SPA):用户需要登录Azure AD保护的单页应用程序。
  4.   
  5. 原生应用程序到Web API:在手机,平板电脑或PC上运行的本机应用程序需要对用户进行身份验证,以便从Azure AD保护的Web API获取资源。
  6.   
  7. Web应用程序到Web API:Web应用程序需要从Azure AD保护的Web API获取资源。
  8.   
  9. 守护程序或Web应用程序的Web应用程序:守护程序应用程序或没有Web用户界面的服务器应用程序需要从Azure AD保护的Web API获取资源。
  10.   

您提到您已注册本机应用程序。我假设您需要对Azure Active Directory(从现在开始的AAD)进行身份验证才能访问受保护的Web api或Web应用程序(场景#3),因此您还必须注册该域名。

static void Main(string[] args)
{
    GetTokenAsync().Wait();
}

static async Task<string> GetTokenAsync()
{
    string Tenant = "mytest.onmicrosoft.com";
    string Authority = "https://login.microsoftonline.com/" + Tenant;
    string GatewayLoginUrl = "https://login.microsoftonline.com/something/wsfed";
    string ClientId = "something";
    Uri RedirectUri = new Uri("http://something");

    AuthenticationContext context = new AuthenticationContext(Authority);
    PlatformParameters platformParams = new PlatformParameters(PromptBehavior.Auto, null);
    AuthenticationResult result = await context.AcquireTokenAsync(GatewayLoginUrl, ClientId, RedirectUri, platformParams);

    return result.ToString();
}
  • Tenant是AAD域名的名称,似乎你没有那个
  • Authority"https://login.microsoftonline.com/" + Tenant,所以看起来你也是这样的
  • GatewayLoginUrl是您正在保护的应用程序的App Id Uri
  • ClientId是本机应用程序的应用程序ID
  • RedirectUri是本机应用程序的重定向Uri

申请保护:

enter image description here

你从这里得到GatewayLoginUrl

访问要保护的应用程序的本机应用程序:

enter image description here

您可以从此处获得ClientIdRedirectUri

其他参考

您可以看到本机应用程序here

的完整演练

有关使用本机应用访问受AAD保护的应用程序的全局概述,请参阅the docs