【问题标题】:Authentication using ADAL使用 ADAL 进行身份验证
【发布时间】:2018-01-03 11:05:11
【问题描述】:

我正在使用以下代码对我的 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();
    }

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

  • 租户
  • 权威
  • 网关登录网址
  • 客户标识
  • RedirectUri

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

【问题讨论】:

  • 这取决于您的type of application and scenario。根据您的应用类型,查看其中一个示例 here
  • @PeterBons 到目前为止,我已经注册了一个原生应用程序。为此如何获取 GatewayAccessURL 和 Redirect?
  • 对于重定向,您可以使用 localhost 例如,因为本机应用程序不需要重定向 url。 GatewayLoginUrl 是您可以在客户端应用程序的属性中找到的应用程序 id uri
  • @PeterBons 你能提交这个作为答案吗?带有 GatewayLoginUrl 的截图。

标签: azure azure-active-directory


【解决方案1】:

在使用 Azure Active Directory 保护应用程序时有几种方案(请参阅 here):

以下是 Azure AD 支持的五个主要应用场景:

  1. Web 浏览器到 Web 应用程序:用户需要登录到受 Azure AD 保护的 Web 应用程序。
  2. 单页应用程序 (SPA):用户需要登录到受 Azure AD 保护的单页应用程序。
  3. 本机应用程序到 Web API:在手机、平板电脑或 PC 上运行的本机应用程序需要对用户进行身份验证才能从受 Azure AD 保护的 Web API 获取资源。李>
  4. Web 应用程序到 Web API:Web 应用程序需要从受 Azure AD 保护的 Web API 获取资源。
  5. Web API 的守护程序或服务器应用程序:没有 Web 用户界面的守护程序应用程序或服务器应用程序需要从受 Azure AD 保护的 Web API 获取资源。

你提到你已经注册了一个原生应用程序。我假设您需要针对 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

应用保护:

您可以从这里获得GatewayLoginUrl。

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

您可以从这里获得ClientId 和RedirectUri。

其他参考资料

您可以查看本机应用程序的完整演练here

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

【讨论】:

  • 感谢您的详细回答。我尝试了 Azure 试用帐户。对于我的本机应用程序,我没有看到任何字段:“Application ID Uri”。
  • 没错,原生应用没有这个属性,注册的webapp/api应用有。你能告诉我更多关于你的这个原生应用程序的信息吗?它尝试访问什么 api?
  • 另一个团队正在开发用于用户登录的 WebAPI 服务。我必须编写身份验证代码。我首先假设用户登录服务需要在 AD 中注册。从中我将获得 AppID Uri。稍后我的身份验证代码将对此进行验证。但是我们有一个 Xamarin 登录表单。我需要使用 Native to WebAPI 身份验证还是 Web App to Web API 身份验证?
猜你喜欢
  • 1970-01-01
  • 2017-03-22
  • 1970-01-01
  • 2015-08-09
  • 2018-11-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多