Azure MobileServiceClient LoginAsync

时间:2018-04-23 13:46:14

标签: azure xamarin.android azure-mobile-services

我正在尝试使用Azure移动服务客户端来验证我正在使用Xamarin.Android构建的应用。我的MainActivity具有以下代码:

[Activity(Label = "AppName", MainLauncher = true, Icon = "@mipmap/icon")]
public class MainActivity : Activity
{
    private MobileServiceUser user;
    private MobileServiceClient client;

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        CurrentPlatform.Init();

        client = new MobileServiceClient(Config.AppServiceUrl);

        SetContentView(Resource.Layout.Main);

        FindViewById<Button>(Resource.Id.ListOfEquipmentButton).Click += ListOfEquipmentButton_Click;
        FindViewById<Button>(Resource.Id.buttonLoginUser).Click += LoginUser;
    }

    private async Task<bool> Authenticate()
    {
        var success = false;
        try
        {

            user = await client.LoginAsync(this,
                                           MobileServiceAuthenticationProvider.WindowsAzureActiveDirectory, "https://MyAzureWebServiceSite.azurewebsites.net");
            CreateAndShowDialog(string.Format("you are now logged in - {0}",
                user.UserId), "Logged in!");

            success = true;
        }
        catch (Exception ex)
        {
            CreateAndShowDialog(ex, "Authentication failed");
        }
        return success;
    }

    public async void LoginUser(object sender, EventArgs e)
    {
        // Load data only after authentication succeeds.
        if (await Authenticate())
        {
            //Hide the button after authentication succeeds.
            FindViewById<Button>(Resource.Id.buttonLoginUser).Visibility = ViewStates.Gone;
        }
    }

    void ListOfEquipmentButton_Click(object sender, System.EventArgs e)
    {
        var intent = new Intent(this, typeof(ListOfEquipmentActivity));
        StartActivity(intent);
    }

    private void CreateAndShowDialog(Exception exception, String title)
    {
        CreateAndShowDialog(exception.Message, title);
    }

    private void CreateAndShowDialog(string message, string title)
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);

        builder.SetMessage(message);
        builder.SetTitle(title);
        builder.Create().Show();
    }
}

当我运行这个并点击我在布局上放置的登录按钮时,我得到以下内容:

Web Authenticator Native Browser 我假设它正在尝试加载一个网页,我可以使用我的Azure AD凭据登录,但我没有登录屏幕。有什么想法吗?我的代码是FUBAR得到的吗?任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

  

user = await client.LoginAsync(this,MobileServiceAuthenticationProvider.WindowsAzureActiveDirectory,“https://MyAzureWebServiceSite.azurewebsites.net”);

根据您的代码,我假设您使用的是Microsoft.Azure.Mobile.Client版本4.x.x.目前,您需要正确设置url_scheme_of_your_app

  • 通过Azure门户在允许的外部重定向网址下设置<url_scheme_of_your_app>://easyauth.callback

  • AndroidManifest.xml文件下,在application XML元素中添加相关代码。

您可以关注Add authentication to your Xamarin.Android app的详细信息。

此外,您还可以尝试将Microsoft.Azure.Mobile.Client包降级为3.1.0,而无需设置上述设置。