试图通过Azure AD登录用户在UWP中失败
我遵循这一点,通过我的UWP中的AAD对我的用户进行身份验证。在门户中,我注册了两个广告,一个是web应用程序“testservice”,另一个是本地应用程序“testservicenative”,其中testservicenative拥有testservice的权限。我还在“testservice”中指定了我希望他登录的用户。以下是我在应用程序中编写的代码:试图通过Azure AD登录用户在UWP中失败,azure,win-universal-app,uwp,Azure,Win Universal App,Uwp,我遵循这一点,通过我的UWP中的AAD对我的用户进行身份验证。在门户中,我注册了两个广告,一个是web应用程序“testservice”,另一个是本地应用程序“testservicenative”,其中testservicenative拥有testservice的权限。我还在“testservice”中指定了我希望他登录的用户。以下是我在应用程序中编写的代码: string clientId = "34324453-4354-4534-5454-4564565464";
string clientId = "34324453-4354-4534-5454-4564565464";
string authority = "https://login.microsoftonline.com/testtenant.onmicrosoft.com";
string resourceUri = "http://testservice.azurewebsites.net";
string message;
while (user == null)
{
try
{
AuthenticationContext ac = new AuthenticationContext(authority);
PlatformParameters par = new PlatformParameters(PromptBehavior.Auto, false);
AuthenticationResult ar = await ac.AcquireTokenAsync(resourceUri, clientId, (Uri)null, par);
JObject payload = new JObject();
payload["access_token"] = ar.AccessToken;
user = await MobileService.LoginAsync(MobileServiceAuthenticationProvider.WindowsAzureActiveDirectory, payload);
message = string.Format("You are now logged in - {0}", user.UserId);
}
catch (InvalidOperationException ex)
{
message = "You must log in. Login Required";
}
catch(Exception ex)
{
message = "You must log in. Login Required";
}
var dialog = new MessageDialog(message);
dialog.Commands.Add(new UICommand("OK"));
await dialog.ShowAsync();
}
我正在使用我的用户数据和AuthenticationTok获取AuthenticationResult。然而,当调用LoginAsync时,它抛出一个
InvalidOperationException,消息为=“请求无法完成。(未经授权)”
我认为问题在于传递给
AcquireTokenAsync
方法的(Uri)null
。通常,您需要在Azure AD中注册应用程序的回调Uri。然后,您必须在此处将相同的Uri传递给此方法。通过调用Windows.Security.Authentication.Web.WebAuthenticationBroker.GetCurrentApplicationCallbackUri()
可以找到正确的CallbackUri。我认为问题在于传递给AcquireTokenAsync
方法的(Uri)null
。通常,您需要在Azure AD中注册应用程序的回调Uri。然后,您必须在此处将相同的Uri传递给此方法。通过调用Windows.Security.Authentication.Web.WebAuthenticationBroker.GetCurrentApplicationCallbackUri()