6

如何实现 Linkedin 的登录方法,以便人们只需单击一个按钮并使用他们的 Linkedin 帐户登录,就像在 Facebook 或 Twitter 上一样?两者都使用 OAuth,但我发现为它们指定的库非常易于使用。对于 Linkedin,我只在 DotNetOpenAuth 中找到了一些示例代码,但我对此毫无意义

我可以使用任何库来促进 Linkedin 的登录功能吗?或者有关如何在 ASP.NET MVC 中使用 DotNetOpenAuth 4 执行此操作的任何教程?

4

2 回答 2

4

这是看起来非常可靠的样本

http://mrsarker.wordpress.com/2011/08/20/linkedin-rest-api-in-asp-net-mvc/

[HandleError]
public class LinkedInController : Controller
{
    public ActionResult index()
    {
        return AuthenticateToLinkedIn();
    }

    static string token_secret = "";
    public ActionResult AuthenticateToLinkedIn()
    {
        var credentials = new OAuthCredentials
        {
            CallbackUrl = "http://localhost/home/callback",
            ConsumerKey = ConfigurationManager.AppSettings["ConsumerKey"],
            ConsumerSecret = ConfigurationManager.AppSettings["ConsumerSecret"],
            Verifier = "123456",
            Type = OAuthType.RequestToken
        };

        var client = new RestClient { Authority = "https://api.linkedin.com/uas/oauth", Credentials = credentials };
        var request = new RestRequest { Path = "requestToken" };
        RestResponse response = client.Request(request);

        token = response.Content.Split('&').Where(s => s.StartsWith("oauth_token=")).Single().Split('=')[1];
        token_secret = response.Content.Split('&').Where(s => s.StartsWith("oauth_token_secret=")).Single().Split('=')[1];
        Response.Redirect("https://api.linkedin.com/uas/oauth/authorize?oauth_token=" + token);
        return null;
    }

    string token = "";
    string verifier = "";
    public ActionResult Callback()
    {
        token = Request["oauth_token"];
        verifier = Request["oauth_verifier"];
        var credentials = new OAuthCredentials
        {
            ConsumerKey = ConfigurationManager.AppSettings["ConsumerKey"],
            ConsumerSecret = ConfigurationManager.AppSettings["ConsumerSecret"],
            Token = token,
            TokenSecret = token_secret,
            Verifier = verifier,
            Type = OAuthType.AccessToken,
            ParameterHandling = OAuthParameterHandling.HttpAuthorizationHeader,
            SignatureMethod = OAuthSignatureMethod.HmacSha1,
            Version = "1.0"
        };

        var client = new RestClient { Authority = "https://api.linkedin.com/uas/oauth", Credentials = credentials, Method = WebMethod.Post };
        var request = new RestRequest { Path = "accessToken" };
        RestResponse response = client.Request(request);
        string content = response.Content;


        string accessToken = response.Content.Split('&').Where(s => s.StartsWith("oauth_token=")).Single().Split('=')[1];
        string accessTokenSecret = response.Content.Split('&').Where(s => s.StartsWith("oauth_token_secret=")).Single().Split('=')[1];

        var company = new LinkedInService(accessToken, accessTokenSecret).GetCompany(162479);            

        // Some commented call to API
        //company = new LinkedInService(accessToken, accessTokenSecret).GetCompanyByUniversalName("linkedin");
       //  var companies = new LinkedInService(accessToken, accessTokenSecret).GetCompaniesByEmailDomain("apple.com");            
       // var companies1 = new LinkedInService(accessToken, accessTokenSecret).GetCompaniesByEmailDomain("linkedin.com");           
       // var companies2= new LinkedInService(accessToken, accessTokenSecret).GetCompaniesByIdAnduniversalName("162479", "linkedin");
        //var people = new LinkedInService(accessToken, accessTokenSecret).GetPersonById("f7cp5sKscd");
        //var people = new LinkedInService(accessToken, accessTokenSecret).GetCurrentUser();

        //string url = Url.Encode("http://bd.linkedin.com/pub/rakibul-islam/37/522/653");
        //var people = new LinkedInService(accessToken, accessTokenSecret).GetPeoPleByPublicProfileUrl(url);
        //var peopleSearchresult = new LinkedInService(accessToken, accessTokenSecret).SearchPeopleByKeyWord("Princes");

        var peopleSearchresult = new LinkedInService(accessToken, accessTokenSecret).GetPeopleByFirstName("Mizan");
        String companyName = company.Name;
        return Content(companyName);            
    }
}


public class LinkedInService
{
    private const string URL_BASE = "http://api.linkedin.com/v1";
    public static string ConsumerKey { get { return ConfigurationManager.AppSettings["ConsumerKey"]; } }
    public static string ConsumerKeySecret { get { return ConfigurationManager.AppSettings["ConsumerSecret"]; } }
    public string AccessToken { get; set; }
    public string AccessTokenSecret { get; set; }

    public LinkedInService(string accessToken, string accessTokenSecret)
    {
        this.AccessToken = accessToken;
        this.AccessTokenSecret = accessTokenSecret;
    }

    private OAuthCredentials AccessCredentials
    {
        get
        {
            return new OAuthCredentials
            {
                Type = OAuthType.AccessToken,
                SignatureMethod = OAuthSignatureMethod.HmacSha1,
                ParameterHandling = OAuthParameterHandling.HttpAuthorizationHeader,
                ConsumerKey = ConsumerKey,
                ConsumerSecret = ConsumerKeySecret,
                Token = AccessToken,
                TokenSecret = AccessTokenSecret
            };
        }
    }

    #region Helper

    private RestResponse GetResponse(string path)
    {
        var client = new RestClient()
        {
            Authority = URL_BASE,
            Credentials = AccessCredentials,
            Method = WebMethod.Get
        };

        var request = new RestRequest { Path = path };

        return client.Request(request);
    }

    private T Deserialize(string xmlContent)
    {
        MemoryStream memoryStream = new MemoryStream(Encoding.ASCII.GetBytes(xmlContent));
        XmlSerializer deserializer = new XmlSerializer(typeof(T));
        return (T)deserializer.Deserialize(new StringReader(xmlContent));
    }

    #endregion

    // methods removed for brevity. check the original link for full source

}
于 2012-07-11T18:03:58.017 回答
0

如果您不想自己编写代码,您可以随时查看第三方解决方案,例如 Janrain 的 RPX 解决方案:http: //developers.janrain.com/。它会给你LinkedIn登录以及更多。

于 2012-07-11T18:27:57.883 回答