【问题标题】:C# Asp.net Linkedin authorization using Linkedintoolkit with DotNetOpenAuthC# Asp.net Linkedin 授权使用 Linkedintoolkit 和 DotNetOpenAuth
【发布时间】:2011-10-17 23:06:51
【问题描述】:

我正在尝试授权我的 ASP.net 应用程序使用 Linkdin API,但似乎无法正确进行身份验证。我希望有人可以看看我的源代码,看看我可能做错了什么。

首先,我正在尝试按照Linkedintoolkit Documentation 中的步骤进行操作,尽管我正在按照这些步骤操作,但我仍然没有运气。

我理解的过程,最简单的说法如下。

  1. 创建 DotNetOpenAuth 的 IConsumerTokenManager 的实现
  2. 使用 WebOAuthAuthorization 类对我的应用程序进行授权,并为将来的 API 调用返回访问令牌。

我的IConsumerTokenManager实现如下

我在这个例子中添加了一个假的 api 密钥和密钥

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DotNetOpenAuth.OAuth.ChannelElements;
using DotNetOpenAuth.OAuth.Messages;

namespace UserLibrary.SocialMedia
{
    public class ConsumerTokenManager : IConsumerTokenManager
    {


        private const string API_KEY = "ylk7j2jq4j7l";
        private const string SECRET_KEY = "WH3dlPFhPyWG0xlw";

        private Dictionary<string, string> tokensAndSecrets = new Dictionary<string, string>();

        public ConsumerTokenManager()
        {
            ConsumerKey = API_KEY;
            ConsumerSecret = SECRET_KEY;
        }


        #region ITokenManager Members

        public string ConsumerKey { get; private set; }

        public string ConsumerSecret { get; private set; }

        public string GetTokenSecret(string token)
        {
            return this.tokensAndSecrets[token];
        }

        public void StoreNewRequestToken(UnauthorizedTokenRequest request, ITokenSecretContainingMessage response)
        {
            this.tokensAndSecrets[response.Token] = response.TokenSecret;
        }

        public void ExpireRequestTokenAndStoreNewAccessToken(string consumerKey, string requestToken, string accessToken, string accessTokenSecret)
        {
            this.tokensAndSecrets.Remove(requestToken);
            this.tokensAndSecrets[accessToken] = accessTokenSecret;
        }

        /// <summary>
        /// Classifies a token as a request token or an access token.
        /// </summary>
        /// <param name="token">The token to classify.</param>
        /// <returns>Request or Access token, or invalid if the token is not recognized.</returns>
        public TokenType GetTokenType(string token)
        {
            throw new NotImplementedException();
        }

        #endregion

    }
}

这是调用 BeginAuthorization 的代码块

ConsumerTokenManager consumerTokenManager = new ConsumerTokenManager();
            _webOAuthorization = new WebOAuthAuthorization(consumerTokenManager, _accessToken);

        _webOAuthorization.BeginAuthorize();

        _accessToken = _webOAuthorization.CompleteAuthorize();

当调用“BeginAuthorization()”时,我被重定向到“授予 K2PS 发布者访问您的 LinkedIn 帐户“Patrick A”的权限 仅当您使用 LinkedIn 网络信息信任此应用程序时才允许访问”Linkedin 页面并单击“好的,我将允许它”,这会将我返回到我的网络应用程序。但是,“_completeAuthorization()”方法永远不会被命中,其值为 null如果我尝试返回字符串。

另外值得注意的是,我的控制台输出在此过程中引发了线程异常。

下面是我的控制台副本

在 mscorlib.dll 中发生了“System.Threading.ThreadAbortException”类型的第一次机会异常 mscorlib.dll 中出现“System.Threading.ThreadAbortException”类型的异常,但未在用户代码中处理

我对 Oath 还很陌生,但一直在倾注 API,似乎无法解决这个问题。任何帮助将不胜感激。

【问题讨论】:

    标签: c# asp.net oauth linkedin oauth-provider


    【解决方案1】:

    你可以试试这个sample我从here找到的我之前实现成功..

    【讨论】:

      猜你喜欢
      • 2016-09-03
      • 1970-01-01
      • 2013-07-10
      • 2013-10-09
      • 1970-01-01
      • 2016-11-30
      • 2014-05-05
      • 1970-01-01
      • 2012-10-01
      相关资源
      最近更新 更多