Xamarin相当于WebAuthenticationBroker GetCurrentApplicationCallbackUri?

时间:2017-01-07 19:26:51

标签: c# xamarin xamarin.forms adal

我正在尝试验证Xamarin Forms应用程序,但我找不到Windows命名空间类和方法的替代方法:WebAuthenticationBroker.GetCurrentApplicationCallbackUri。

对于Xamarin Forms UWP应用程序,我可以使用它来获取RedirectURL,但我不知道如何处理iOS和Android应用程序。

let content = <UnsafeComponent component={UnsafeComponent}/>;
ReactDOM.render(content, document.getElementById("root"));

谢谢你, 标记

1 个答案:

答案 0 :(得分:0)

事实证明我的Azure AD帐户存在权限问题,但一旦解决了这个问题,这就是我最终使用的Android和iOS解决方案:

将此重定向URI添加到Azure AD App Registration并将其替换为仅限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);