简体   繁体   中英

Method to Change Password for Azure AD B2C in Xamarin Forms

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?

嘿,看起来您尝试直接在图形API上更改密码,我认为这是不允许的,请通过B2C使用resetPassword策略,它将为您处理所有事情

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM