我使用以下代码验证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();
}
我想知道从哪里获取这些值:
这么多代码是否足以使用AD进行用户身份验证?
答案 0 :(得分:2)
使用Azure Active Directory保护应用程序时有几个方案(参见here):
这些是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应用程序的Web应用程序:守护程序应用程序或没有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