身份验证对话框中使用对话框Windows SDK的V2库中的C#桌面应用程序 [英] Authentication to Box in a C# desktop application using the Box Windows SDK v2 library

查看:222
本文介绍了身份验证对话框中使用对话框Windows SDK的V2库中的C#桌面应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看起来这应该是一个简单的事情,但我不能找到一个例子或不够彻底文档看着办吧。

Seems like this should be a simple thing to do, but I can't find an example or thorough enough documentation to figure it out.

我有一个C#桌面应用程序,我想通过API盒盒与整合。我认为用盒子Windows SDK的V2为.NET将要走的路。

I have a C# desktop application that I'd like to integrate with Box via the Box API. I assume that using the Box Windows SDK v2 for .NET will be the way to go.

有人点我到一个简单的,最基本的例子,会为工作一个桌面应用程序?

Can someone point me to a simple, bare-bones example that will work for a desktop application?

推荐答案

我决定尝试和推测这一个我自己。尽管OAuth2用户支持基于非浏览器的认证,显然Box.com已决定不来实现它(或者,至少,我可以找到没有提及如何做到这一点的任何地方)。

I decided to try and figure this one out myself. Even though OAuth2 supports non-browser based authentication, apparently Box.com has decided not to implement it (or, at least, I can find no mention of how to do it anywhere).

因此,对于桌面应用程序基于唯一的选择就是以某种方式拦截发生和猛拉认证信息了查询字符串参数的URL重定向。

So, the only alternative for a desktop based application is to somehow intercept the URL redirect that occurs and yank the authentication information out of the query string parameters.

不过,由于IE浏览器已经下降落伍最近,我在C#和.NET的工作,我决定看看嵌入另一个浏览器,而不是使用内置-in浏览器控件。我去 Awesomium --a管理.NET的包装。

However, as IE has fallen behind the times recently, and I'm working in C# and .NET, I decided to look into embedding another browser rather than using the built-in browser control. I went with Awesomium--a managed .NET Chromium wrapper.

所以,事不宜迟,我提出了最基本的例子,将一个桌面应用程序的工作

So, without further ado, I present the bare-bones example that will work for a desktop application.

我的解决办法有两种形式,一种是我用单纯的浏览器的主要形式有:。frmMain包含了所有的代码和frmBrowser包含Awesomium控制

My solution has two forms, one that I use purely as the "browser" and the main form: frmMain contains all the code and frmBrowser contains the Awesomium control.

using Newtonsoft.Json.Linq;
using System.Web;

private static frmBrowser browser = null;
private const string BoxClientId = "{your client id}";
private const string BoxSecret = "{your secret}";    

private void authenticateWithBox()
{
   browser = new frmBrowser();
   browser.Show();

   browser.webControl1.Source = new Uri("https://www.box.com/api/oauth2/authorize?response_type=code&client_id=" + BoxClientId + "&redirect_uri=https://localsess");
   browser.webControl1.AddressChanged += new Awesomium.Core.UrlEventHandler(webControl1_AddressChanged);
}

void webControl1_AddressChanged(object sender, Awesomium.Core.UrlEventArgs e)
{
  //MessageBox.Show(e.Url.ToString());
  if (e.Url.Host == "localsess")
  {
    NameValueCollection parms = HttpUtility.ParseQueryString(e.Url.Query);
    if (parms.AllKeys.Contains("error"))
    {
       MessageBox.Show("Error connecting to Box.com: " + parms["error"] + " " + parms["error_description"]);
    }
    else
    {
        boxContinue(parms["code"]);
    }
  }
}



上面的代码是哪里奇迹发生。该AddressChanged事件触发每一个由网络控制的变化正在显示的URL的时间。所以,你必须设置你的重定向URL以独特的东西,你可以检测到 - 它甚至没有存在,如图中的示例代码。然后,你可以抽出出来,你需要继续验证过程的参数。

The above code is where the magic happens. The AddressChanged event fires every time the URL being displayed by the web control changes. So you have to set your redirect URL to something unique you can detect--it doesn't even have to exist, as shown in the example code. Then you can just yank out the parameters that you need and continue the authentication process.

string postToUrl(string url, string data)
{
  string results = String.Empty;
  WebRequest req = WebRequest.Create(url);
  req.Method = WebRequestMethods.Http.Post;
  byte[] byteArray = Encoding.UTF8.GetBytes(data);
  req.ContentType = "application/x-www-form-urlencoded";
  req.ContentLength = byteArray.Length;
  Stream dataStream = req.GetRequestStream();
  dataStream.Write(byteArray, 0, byteArray.Length);
  dataStream.Close();
  WebResponse res = req.GetResponse();
  dataStream = res.GetResponseStream();
  StreamReader reader = new StreamReader(dataStream);
  results = reader.ReadToEnd();
  return results;
}

void boxContinue(string code)
{
  browser.Close();
  browser.Dispose();
  string json = postToUrl("https://www.box.com/api/oauth2/token", "code=" + code + "&grant_type=authorization_code&client_id=" + BoxClientId + "&client_secret=" + BoxSecret);
  JToken token = JObject.Parse(json);

  string access_token = (string)token.SelectToken("access_token");
  string refresh_token = (string)token.SelectToken("refresh_token");
}

void boxRefresh(string refresh_token)
{
  string json = postToUrl("https://www.box.com/api/oauth2/token", "grant_type=refresh_token&refresh_token=" + refresh_token + "&client_id=" + BoxClientId + "&client_secret=" + BoxSecret);
  JToken token = JObject.Parse(json);

  string access_token = (string)token.SelectToken("access_token");
  string new_refresh_token = (string)token.SelectToken("refresh_token");
}



代码的剩余部分就是你运行的设施,工厂认证使用先前要求的标记和诸如此类的东西代码以获取更多的标记,等箱采用refresh_tokens,让你获得额外的访问令牌,我显示如何做到这一点,也是一个例子。

The rest of the code is just your run-of-the-mill authentication code that uses the tokens and whatnot from previous requests to get more tokens, etc. Box uses "refresh_tokens" to enable to you get additional access tokens, I show an example of how to do that, too.

如果您发现任何错误或有任何意见,只等发表评论。

If you notice any errors or have any comments, etc. just leave a comment.

这篇关于身份验证对话框中使用对话框Windows SDK的V2库中的C#桌面应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆