2017-09-20 58 views
0

我很难在Xamarin表单中更改密码方法。我试图使用:在Xamarin表单中更改Azure AD B2C的密码的方法

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文件为我的问题,我应该在我的方法添加或我需要别的东西吗?

回答

1

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

+0

以及重置密码/忘记密码政策正在工作,我想要做的是在用户登录后,他们可以更改他们的密码。 –

+0

对不起,误解了,最好的答案是创建一个名为更改密码的自定义策略,它可以让你这样做,除非你想使用已经登录的用户的对象ID,它可以像RESET密码一样工作。一旦你开始使用它们,定制策略非常简单, – user1197563