2017-04-06 46 views
0

任何人都可以帮我解决这个问题吗?OAuth2和UWP Xamarin.Forms

我不知道如何在UWP中使用OAuth2。

例如,在认证的Andriod代码看起来是这样的:

[assembly: ExportRenderer(typeof(LoginPage), typeof(LoginPageRenderer))] 

namespace TestTask.Droid 
{ 
    class LoginPageRenderer : PageRenderer 
    { 
     private static bool _isShown; 
     protected override void OnElementChanged(ElementChangedEventArgs<Page> e) 
     { 
      base.OnElementChanged(e); 

      if (_isShown) return; 
      _isShown = true; 

      var activity = this.Context as Activity; 

      var auth = new OAuth2Authenticator(
       clientId: "someId", 
       scope: "", 
       authorizeUrl: new Uri("https://oauth.vk.com/authorize"), 
       redirectUrl: new Uri("https://oauth.vk.com/blank.html")); 

      auth.Completed += (sender, eventArgs) => { 
       if (eventArgs.IsAuthenticated) 
       { 
        AuthInfo.Token = eventArgs.Account.Properties["access_token"].ToString(); 
        AuthInfo.UserID = eventArgs.Account.Properties["user_id"].ToString(); 
       } 
       else 
       { 
        // The user cancelled 
       } 
      }; 

      activity?.StartActivity((Intent)auth.GetUI(activity)); 
     } 
    } 
} 

所以,Android上的解决方案是在该方法中

protected override void OnElementChanged(ElementChangedEventArgs<Page> e) 

和该行FO代码在

activity?.StartActivity((Intent)auth.GetUI(activity)); 

我的问题是:我如何在UWP中执行相同的操作,或者如何使其在UWP中工作?

回答

0

谢谢大家!我发现了一个针对UWP平台的解决方案。

[assembly: ExportRenderer(typeof(LoginPage), typeof(LoginPageRenderer))] 

namespace TestTask.UWP 
{ 
    public class LoginPageRenderer : PageRenderer 
    { 
     private Windows.UI.Xaml.Controls.Frame _frame; 
     private bool _isShown; 

     protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Page> e) 
     { 
      base.OnElementChanged(e); 

      if (_isShown) return; 
      _isShown = true; 

      if (Control == null) 
      { 
       WindowsPage windowsPage = new WindowsPage(); 

       var auth = new OAuth2Authenticator(
        clientId: "someID", 
        scope: "", 
        authorizeUrl: new Uri("https://oauth.vk.com/authorize"), 
        redirectUrl: new Uri("https://oauth.vk.com/blank.html")); 

       _frame = windowsPage.Frame; 
       if (_frame == null) 
       { 
        _frame = new Frame(); 
        //_frame.Language = global::Windows.Globalization.ApplicationLanguages.Languages[0]; 

        windowsPage.Content = _frame; 
        SetNativeControl(windowsPage); 
       } 

       auth.Completed += (sender, eventArgs) => { 

        if (eventArgs.IsAuthenticated) 
        { 
         AuthInfo.Token = eventArgs.Account.Properties["access_token"].ToString(); 
         AuthInfo.UserID = eventArgs.Account.Properties["user_id"].ToString(); 
        } 
        else 
        { 
         // The user cancelled 
        } 
       }; 

       Type pageType = auth.GetUI(); 
       _frame.Navigate(pageType, auth); 
       Window.Current.Activate(); 
      } 
     } 
    } 
} 

请标记解决!

0

我的问题是:如何在UWP中执行相同的操作,或者如何使其在UWP中工作?

我不知道该Xamarin.Auth是否支持UWP与否,但UWP,我们可以使用Web authentication broker对OAuth的,你可以检查的官方WebAuthenticationBroker sample,这不是xamarin,但你可以在本地UWP项目和使用它的代码自定义渲染器在Xamarin中执行相同的操作。

0

Xamarin.Auth支持标准/传统的UWP相当长。 最近增加了Xamarin.Forms支持,未经彻底测试(1.5.0-alpha)