Azure 使用ADAL进行身份验证

Azure 使用ADAL进行身份验证,azure,azure-active-directory,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:

我正在使用以下代码验证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();
    }
static void Main(字符串[]args)
{
GetTokenAsync().Wait();
}
静态异步任务GetTokenAsync()
{
字符串Tenant=“mytest.onmicrosoft.com”;
字符串权限=”https://login.microsoftonline.com/“+承租人;
字符串GatewayLoginUrl=”https://login.microsoftonline.com/something/wsfed";
字符串ClientId=“something”;
Uri重定向Uri=新Uri(“http://something");
AuthenticationContext=新的AuthenticationContext(授权);
PlatformParameters platformParams=新的PlatformParameters(PromptBehavior.Auto,null);
AuthenticationResult=await context.AcquireTokenAsync(GatewayLoginUrl、ClientId、RedirectUri、platformParams);
返回result.ToString();
}
我想知道从何处获取这些值:

  • 房客
  • 权威
  • 门道酒店
  • 客户
  • 重定向URI

对于使用AD的用户身份验证来说,这么多代码就足够了吗?

在使用Azure Active Directory保护应用程序时,有几种情况(请参阅):

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

  • Web浏览器到Web应用程序:用户需要登录到受Azure AD保护的Web应用程序
  • 单页应用程序(SPA):用户需要登录到受Azure AD保护的单页应用程序
  • 本机应用程序到Web API:在手机、平板电脑或PC上运行的本机应用程序需要对用户进行身份验证,才能从Azure AD保护的Web API获取资源。
  • Web应用程序到Web API:Web应用程序需要从Azure AD保护的Web API获取资源
  • 守护程序或服务器应用程序到Web API:没有Web用户界面的守护程序或服务器应用程序需要从Azure AD保护的Web API获取资源
  • 您提到您已经注册了一个本机应用程序。我假设您需要对Azure Active Directory(AAD)进行身份验证才能访问受保护的web api或web应用程序(场景3),因此您也必须注册该api或web应用程序

    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();
    }
    
    static void Main(字符串[]args)
    {
    GetTokenAsync().Wait();
    }
    静态异步任务GetTokenAsync()
    {
    字符串Tenant=“mytest.onmicrosoft.com”;
    字符串权限=”https://login.microsoftonline.com/“+承租人;
    字符串GatewayLoginUrl=”https://login.microsoftonline.com/something/wsfed";
    字符串ClientId=“something”;
    Uri重定向Uri=新Uri(“http://something");
    AuthenticationContext=新的AuthenticationContext(授权);
    PlatformParameters platformParams=新的PlatformParameters(PromptBehavior.Auto,null);
    AuthenticationResult=await context.AcquireTokenAsync(GatewayLoginUrl、ClientId、RedirectUri、platformParams);
    返回result.ToString();
    }
    
    • Tenant
      是AAD域的名称,看起来您对了
    • 权限
      ”https://login.microsoftonline.com/“+Tenant
      ,看来你也做对了
    • GatewayLoginUrl
      是您要保护的应用程序的应用程序Id Uri
    • ClientId
      是本机应用程序的应用程序Id
    • RedirectUri
      是本机应用程序的重定向Uri
    要保护的应用程序:

    您可以从这里获得
    GatewayLoginUrl

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

    您可以从这里获得
    ClientId
    RedirectUri

    其他参考资料

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


    有关使用本机应用程序访问受AAD保护的应用程序的全局概述,请参阅使用Azure Active Directory保护应用程序时,有几个场景(请参阅):

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

  • Web浏览器到Web应用程序:用户需要登录到受Azure AD保护的Web应用程序
  • 单页应用程序(SPA):用户需要登录到受Azure AD保护的单页应用程序
  • 本机应用程序到Web API:在手机、平板电脑或PC上运行的本机应用程序需要对用户进行身份验证,才能从Azure AD保护的Web API获取资源。
  • Web应用程序到Web API:Web应用程序需要从Azure AD保护的Web API获取资源
  • 守护程序或服务器应用程序到Web API:没有Web用户界面的守护程序或服务器应用程序需要从Azure AD保护的Web API获取资源
  • 您提到您已经注册了一个本机应用程序。我假设您需要对Azure Active Directory(AAD)进行身份验证才能访问受保护的web api或web应用程序(场景3),因此您也必须注册该api或web应用程序

    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();
    }
    
    static void Main(字符串[]args)
    {
    GetTokenAsync().Wait();
    }
    静态异步任务GetTokenAsync()
    {
    字符串Tenant=“mytest.onmicrosoft.com”;
    字符串权限=”https://login.microsoftonline.com/“+承租人;
    字符串GatewayLoginUrl=”https://login.microsoftonline.com/something/wsfed";
    字符串ClientId=“something”;
    Uri重定向Uri=新Uri(“http://something");
    AuthenticationContext=新的AuthenticationContext(授权);
    PlatformParameters platformParams=新的PlatformParameters(PromptBehavior.Auto,null);
    AuthenticationResult=await context.AcquireTokenAsync(GatewayLoginUrl、ClientId、RedirectUri、platformParams);
    返回result.ToString();
    }
    
    • 租户