0

I have difficulty to make change password method in Xamarin forms. I tried to use:

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

It's hard to find reference to make it works in Xamarin forms and this is what I have so far.

Here is my model:

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


    }
}

...and here is my user interface for change password:

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

...and here is my method so far:

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();
        }


    }
}

The result is parameter from my login view model, and here is my 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
        }
    }
}

Is there any reference I can look or GitHub file for my problem, and what should I add here in my method or do I need something else?

Saca
  • 10,355
  • 1
  • 34
  • 47
Theodorus Agum Gumilang
  • 1,414
  • 4
  • 23
  • 46

2 Answers2

2

Hey it looks like your trying to change the password directly on graph API, I dont think this is allowed, use a resetPassword policy through B2C which will handle everything for you

whatisthejava
  • 481
  • 3
  • 12
  • well the Reset Password / Forgot Password policy is working, what i want to do is after the user sign in they can change their password. – Theodorus Agum Gumilang Sep 20 '17 at 12:52
  • Ahh sorry misunderstood, the best answer is to create a custom policy called change password that will let you do this , it works like RESET password except you want to use the object ID of the user already logged in . The custom policies are pretty straight forward once you start using them, – whatisthejava Sep 21 '17 at 13:17
0

You can change the password via the Azure AD Graph API. See solution 2 from this SO question.

You can't use Microsoft Graph API at this point to manage B2C users. See this SO question for additional details.

spottedmahn
  • 14,823
  • 13
  • 108
  • 178