我正在使用此逐步指南(https://blog.xamarin.com/authenticate-mobile-apps-using-microsoft-authentication-library/)来实现我的应用程序的Microsoft登录页面,用户必须在其中使用Microsoft帐户登录。但是我已经走到第三步了,找不到摆脱这些错误的方法。
XAML中的代码:
<StackLayout HorizontalOptions="Center" VerticalOptions="Center">
<Button Text="Login" x:Name="LoginButton"/>
</StackLayout>
</ContentPage.Content>
(C#)后面的代码中的代码:
using System;
using System.Collections.Generic;
using Microsoft.Identity.Client;
using Xamarin.Forms;
namespace RoseySports
{
public partial class Login : ContentPage
{
public IPlatformParameters PlatformParameters { get; set; }
public Login()
{
InitializeComponent();
LoginButton.Clicked += LoginButton_Clicked;
}
protected override void OnAppearing()
{
App.ClientApplication.PlatformParameters = PlatformParameters;
base.OnAppearing();
}
private async void LoginButton_Clicked(object sender, EventArgs e)
{
try
{
AuthenticationResult ar = await App.ClientApplication.AcquireTokenAsync(App.Scopes);
WelcomeText.Text = $"Welcome {ar.User.Name}";
}
catch (MsalException ex)
{
WelcomeText.Text = ex.Message;
}
}
}
}
App.xaml.cs中的代码:
using Xamarin.Forms;
using Microsoft.Identity.Client;
namespace RoseySports
{
public partial class App : Application
{
public static PublicClientApplication ClientApplication { get; set; }
public static string[] Scopes = { "User.Read" };
public App()
{
InitializeComponent();
ClientApplication = new PublicClientApplication("your-app-id");
var content = new Login();
MainPage = new NavigationPage(content);
MainPage = new Login_Page();
}
protected override void OnStart()
{
// Handle when your app starts
}
protected override void OnSleep()
{
// Handle when your app sleeps
}
protected override void OnResume()
{
// Handle when your app resumes
}
}
}
这是所有错误的屏幕截图:error 1 error 2 error 3 error 4
另外,有人可以向我解释该代码放在哪里:
[assembly: ExportRenderer(typeof(Login), typeof(LoginPageRenderer))]
namespace MSALForForms.iOS
{
class LoginPageRenderer : PageRenderer
{
Login _page;
protected override void OnElementChanged(VisualElementChangedEventArgs e)
{
base.OnElementChanged(e);
_page = e.NewElement as Login;
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
_page.PlatformParameters = new PlatformParameters(this);
}
}
}
答案 0 :(得分:0)
根据屏幕截图错误,您应该添加Microsoft.IdentityModel.Clients.ActiveDirectory
,因为您缺少此参考。您可以通过VS中的NuGet包添加它。
using Microsoft.IdentityModel.Clients.ActiveDirectory;