所以不确定你是否在Android,iOS或Xamarin.Forms上这样做.下面是我将如何使用ADAL和Azure进行身份验证(代码正在我的工作中):
在Android上:
public async TaskAuthenticate(Activity context, string authority, string resource, string clientId, string returnUri) { var authContext = new AuthenticationContext(authority); if (authContext.TokenCache.ReadItems().Any()) authContext = new AuthenticationContext(authContext.TokenCache.ReadItems().First().Authority); var uri = new Uri(returnUri); var platformParams = new PlatformParameters(context); try { var authResult = await authContext.AcquireTokenAsync(resource, clientId, uri, platformParams); return authResult; } catch (AdalException e) { return null; } }
在iOS上:
public async TaskAuthenticate(UIViewController controller, string authority, string resource, string clientId, string returnUri) { var authContext = new AuthenticationContext(authority); if (authContext.TokenCache.ReadItems().Any()) authContext = new AuthenticationContext(authContext.TokenCache.ReadItems().First().Authority); var controller = UIApplication.SharedApplication.KeyWindow.RootViewController; var uri = new Uri(returnUri); var platformParams = new PlatformParameters(controller); try { var authResult = await authContext.AcquireTokenAsync(resource, clientId, uri, platformParams); return authResult; } catch (AdalException e) { return null; } }
在UWP上:
public async TaskAuthenticate(string authority, string resource, string clientId, string returnUri) { var authContext = new AuthenticationContext(authority); if (authContext.TokenCache.ReadItems().Any()) authContext = new AuthenticationContext(authContext.TokenCache.ReadItems().First().Authority); var uri = new Uri(returnUri); var platformParams = new PlatformParameters(PromptBehavior.Auto); try { var authResult = await authContext.AcquireTokenAsync(resource, clientId, uri, platformParams); return authResult; } catch (AdalException e) { return null; } }
我传入上述方法的变量:
string authority = "https://login.windows.net/common"; string ResourceID = "Backend ClientId";//Backend (web app) string clientId = "Native App ClientId";//native app string returnUri = "https://{My Azure Site}.azurewebsites.net/.auth/login/done";
如果你想在Xamarin.Forms中这样做,下面是我的GitHub解决方案的链接,我已经通过这些方法公开了这些方法DependencyService
.
PCL实施
iOS实现
Android实施
我希望这有帮助!如果您的响应中出现任何错误,请检查以确保您在Azure中正确设置了权限.我是这样做的.另一个很好的资源是Adrian Hall的Xamarin/Azure书