小编mka*_*ner的帖子

WPF和C#中的Facebook OAuth示例

我正在开发一个WPF应用程序,要求我使用oAuthFacebook获取访问令牌.经过大量在线搜索后,我得出以下结论:

  1. OAuth必须在浏览器中完成
  2. 我需要在该浏览器中查看URL帖子,因此它必须在WebBrowser WPF控件中

我决定创建一个用于进行Facebook身份验证的模态对话框,我可以使用访问令牌而忽略其余部分.我想继续使用MVVM模型,但它比我预期的要困难.任何关于如何做到这一点的想法都会非常有帮助

以下是我实现的一些功能

  • Cookie删除因此可以让另一个用户进行身份验证,而无需将当前用户注销
  • 禁用新帐户创建,因为它会导致奇怪的UI体验
  • 听取 Facebook生成的javascript中的取消按钮

WPF窗口

WPF非常简单.本质上它只是一个WebBrowser控件,其中连接了Navigated和Navigating事件.

<Window x:Class="FacebookAuthenticator.FacebookAuthenticationWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Authenticate Facebook" 
        Height="600" 
        Width="600" 
        ResizeMode="NoResize" 
        WindowStyle="ToolWindow">
<Grid>
    <WebBrowser Name="webBrowser" 
                Navigated="webBrowser_Navigated" 
                Navigating="webBrowser_Navigating" />
</Grid>
Run Code Online (Sandbox Code Playgroud)

代码

//The Application ID from Facebook
public string AppID {get; set; }

//The access token retrieved from facebook's authentication
public string AccessToken {get; set; }

public FacebookAuthenticationWindow()
{
    InitializeComponent();
    this.Loaded += (object sender, RoutedEventArgs e) =>
    {
        //Add the message hook in …
Run Code Online (Sandbox Code Playgroud)

c# wpf facebook oauth

27
推荐指数
0
解决办法
7042
查看次数

标签 统计

c# ×1

facebook ×1

oauth ×1

wpf ×1