在Xamarin Forms中更改Azure AD B2C密码的方法

时间:2017-09-20 11:37:51

标签: azure xamarin.forms azure-ad-b2c

我很难在Xamarin表单中进行更改密码方法。我试着用:

https://graph.windows.net/me/changePassword?api-version=1.6

很难找到让它以Xamarin形式工作的参考,这就是我到目前为止所做的。

这是我的模特:

using Newtonsoft.Json;

namespace KGVC.Models
{
    public class GraphModel
    {
        const  string ChangePassword = "https://graph.windows.net/me/changePassword?api-version=1.6";
        [JsonProperty("currentPassword")]
        public static string  currentPassword { get; set; }

        [JsonProperty("newPassword")]
        public static string newPassword { get; set; }


    }
}

...这是我更改密码的用户界面:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="KGVC.Views.LogoutPage">
    <StackLayout HorizontalOptions="Center" VerticalOptions="Center">
        <Button x:Name="logoutButton" Text="Logout" Clicked="OnLogoutButtonClicked" />
        <Label x:Name="messageLabel" FontSize="Medium" />
        <Label Text="Change Password" FontSize="Large" HorizontalOptions="Center" Margin="5"/>
        <Label Text="Current Password"  VerticalOptions="Center"  Margin="5"/>
        <Entry x:Name="currentPassword"/>
        <Label Text="New Password"  VerticalOptions="Center"/>
        <Entry x:Name="newPassword"/>
        <Button Text="Change Password" Clicked="ChangePasswordClicked" Margin="20"/>
    </StackLayout>
</ContentPage>

......到目前为止,这是我的方法:

using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using KGVC.Models;
using Microsoft.Identity.Client;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace KGVC.Views
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class LogoutPage : ContentPage
    {

        AuthenticationResult authenticationResult;

        public LogoutPage(AuthenticationResult result)
        {
            InitializeComponent();
            authenticationResult = result;
        }

        protected override void OnAppearing()
        {
            if (authenticationResult != null)
            {
                if (authenticationResult.User.Name != "unknown")
                {
                    messageLabel.Text = string.Format("Welcome {0}", authenticationResult.User.Name);
                }
                else
                {
                    messageLabel.Text = string.Format("UserId: {0}", authenticationResult.User.UniqueId);
                }
            }

            base.OnAppearing();
        }
        public  void ChangePasswordClicked(object sender, EventArgs e)
        {
            var client = new HttpClient();
            var request = new HttpRequestMessage(HttpMethod.Post,
                "https://graph.windows.net/me/changePassword?api-version=1.6");
          //  request.Headers.Authorization =
            //  new AuthenticationHeaderValue("Bearer", authResult.AccessToken);
            //var response = await client.SendAsync(request);
            //var content = await response.Content.ReadAsStringAsync();
        }

        async void OnLogoutButtonClicked(object sender, EventArgs e)
        {
            App.AuthenticationClient.UserTokenCache.Clear(Constants.ApplicationID);
            await Navigation.PopAsync();
        }


    }
}

结果是来自我的登录视图模型的参数,这是我的App.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KGVC.Models;
using KGVC.Views;
using Microsoft.Identity.Client;
using Microsoft.Practices.ServiceLocation;
using Microsoft.Practices.Unity;
using Xamarin.Forms;

namespace KGVC
{
    public partial class App : Application
    {

        public static PublicClientApplication AuthenticationClient { get; private set; }
        public App()
        {
            InitializeComponent();
            UnityContainer unityContainer = new UnityContainer();
            //  unityContainer.RegisterType<LoginService>();
            ServiceLocator.SetLocatorProvider(() => new UnityServiceLocator(unityContainer));
            AuthenticationClient = new PublicClientApplication(Constants.ApplicationID);
            MainPage = new NavigationPage(new LoginPage());
        }

        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
        }
    }
}

我可以查看任何引用或GitHub文件以解决我的问题,我应该在我的方法中添加什么,还是需要其他内容?

2 个答案:

答案 0 :(得分:1)

嘿,看起来你试图直接在图谱API上更改密码,我不认为这是允许的,通过B2C使用resetPassword策略,它将为你处理一切

答案 1 :(得分:0)

您可以通过Azure AD Graph API更改密码。 See solution 2 from this SO question

此时您无法使用Microsoft Graph API来管理B2C用户。 See this SO question了解更多详情。