Xamarin 相当于 WebAuthenticationBroker GetCurrentApplicationCallbackUri?
Xamarin equivalent to WebAuthenticationBroker GetCurrentApplicationCallbackUri?
我正在尝试对 Xamarin Forms 应用程序进行身份验证,但找不到 Windows 命名空间 class 和方法的替代方法:WebAuthenticationBroker.GetCurrentApplicationCallbackUri.
对于 Xamarin Forms UWP 应用程序,我可以使用它来获取 RedirectURL,但我不确定如何处理 iOS 和 Android 应用程序。
class Authenticator : IAuthenticator
{
/// <summary>
/// Address to return to upon receiving a response from the authority.
/// </summary>
private static readonly Uri RedirectURI = WebAuthenticationBroker.GetCurrentApplicationCallbackUri();
public async Task<AuthenticationResult> AuthenticateAsync(UserLogOn user, string authority, string resource, Uri redirectURI, string clientId)
{
PlatformParameters platformParams = new PlatformParameters(PromptBehavior.Always, false);
try
{
var authContext = new AuthenticationContext(authority);
var authResult = await authContext.AcquireTokenAsync(resource, clientId, RedirectURI, platformParams);
return authResult;
}
catch (Exception e)
{
Debug.WriteLine($"AquireTokenAsync Error: {e.Message}");
return null;
}
}
}
谢谢,
马克
事实证明我的 Azure AD 帐户存在权限问题,但一旦问题得到解决,这就是我最终使用的 Android 和 iOS 的解决方案:
将此重定向 URI 添加到 Azure AD 应用程序注册并将其替换为 Windows 唯一方法 GetCurrentApplicationCallbackUri 成功了。
/// <summary>
/// Redirect URI for Android and iOS.
/// </summary>
public static Uri RedirectURI = new Uri("http://AppNamespace.azurewebsites.net/local");
在这里,我在项目中使用了一个接口:
Android
class Authenticator : IAuthenticator
{
public async Task<AuthenticationResult> AuthenticateAsync(UserLogOn user, string authority, string resource, Uri redirectURI, string clientId)
{
try
{
var authContext = new AuthenticationContext(authority);
var authResult = await authContext.AcquireTokenAsync(resource, clientId, redirectURI, new PlatformParameters((Activity)Forms.Context));
return authResult;
}
catch (Exception)
{
return null;
}
}
}
iOS
class Authenticator : IAuthenticator
{
public async Task<AuthenticationResult> AuthenticateAsync(UserLogOn user, string authority, string resource, Uri redirectURI, string clientId)
{
try
{
var authContext = new AuthenticationContext(authority);
var authResult = await authContext.AcquireTokenAsync(resource, clientId, redirectURI,
new PlatformParameters(UIApplication.SharedApplication.KeyWindow.RootViewController));
return authResult;
}
catch (Exception)
{
throw;
}
}
}
最后我用这一行调用接口:
var authResult = await DependencyService.Get<IAuthenticator>().AuthenticateAsync(user, QueryParameters.Authority, QueryParameters.GraphResourceUri, QueryParameters.RedirectURI, QueryParameters.ClientId);
我正在尝试对 Xamarin Forms 应用程序进行身份验证,但找不到 Windows 命名空间 class 和方法的替代方法:WebAuthenticationBroker.GetCurrentApplicationCallbackUri.
对于 Xamarin Forms UWP 应用程序,我可以使用它来获取 RedirectURL,但我不确定如何处理 iOS 和 Android 应用程序。
class Authenticator : IAuthenticator
{
/// <summary>
/// Address to return to upon receiving a response from the authority.
/// </summary>
private static readonly Uri RedirectURI = WebAuthenticationBroker.GetCurrentApplicationCallbackUri();
public async Task<AuthenticationResult> AuthenticateAsync(UserLogOn user, string authority, string resource, Uri redirectURI, string clientId)
{
PlatformParameters platformParams = new PlatformParameters(PromptBehavior.Always, false);
try
{
var authContext = new AuthenticationContext(authority);
var authResult = await authContext.AcquireTokenAsync(resource, clientId, RedirectURI, platformParams);
return authResult;
}
catch (Exception e)
{
Debug.WriteLine($"AquireTokenAsync Error: {e.Message}");
return null;
}
}
}
谢谢, 马克
事实证明我的 Azure AD 帐户存在权限问题,但一旦问题得到解决,这就是我最终使用的 Android 和 iOS 的解决方案:
将此重定向 URI 添加到 Azure AD 应用程序注册并将其替换为 Windows 唯一方法 GetCurrentApplicationCallbackUri 成功了。
/// <summary>
/// Redirect URI for Android and iOS.
/// </summary>
public static Uri RedirectURI = new Uri("http://AppNamespace.azurewebsites.net/local");
在这里,我在项目中使用了一个接口:
Android
class Authenticator : IAuthenticator
{
public async Task<AuthenticationResult> AuthenticateAsync(UserLogOn user, string authority, string resource, Uri redirectURI, string clientId)
{
try
{
var authContext = new AuthenticationContext(authority);
var authResult = await authContext.AcquireTokenAsync(resource, clientId, redirectURI, new PlatformParameters((Activity)Forms.Context));
return authResult;
}
catch (Exception)
{
return null;
}
}
}
iOS
class Authenticator : IAuthenticator
{
public async Task<AuthenticationResult> AuthenticateAsync(UserLogOn user, string authority, string resource, Uri redirectURI, string clientId)
{
try
{
var authContext = new AuthenticationContext(authority);
var authResult = await authContext.AcquireTokenAsync(resource, clientId, redirectURI,
new PlatformParameters(UIApplication.SharedApplication.KeyWindow.RootViewController));
return authResult;
}
catch (Exception)
{
throw;
}
}
}
最后我用这一行调用接口:
var authResult = await DependencyService.Get<IAuthenticator>().AuthenticateAsync(user, QueryParameters.Authority, QueryParameters.GraphResourceUri, QueryParameters.RedirectURI, QueryParameters.ClientId);