繁体   English   中英

更改Xamarin形式的Azure AD B2C密码的方法

[英]Method to Change Password for Azure AD B2C in Xamarin Forms

我很难以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文件,可以解决我的问题吗?我应该在此处添加什么方法?还是需要其他东西?

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

您可以通过Azure AD Graph API更改密码。 请参阅此SO问题的解决方案2

您目前无法使用Microsoft Graph API来管理B2C用户。 有关其他详细信息, 请参见此SO问题

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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