与LinkedIn登陆 [英] Login with Linkedin

查看:326
本文介绍了与LinkedIn登陆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个人怎么会实施LinkedIn登录方法,让人们只需点击一个按钮,并用自己的LinkedIn帐号登录,就像在Facebook或Twitter?无论使用OAuth,但我发现指定库那已经死了简单易用。对于LinkedIn我只找到了一些示例code在DotNetOpenAuth但我不能做的任意的感觉出来。

How would one implement a login method for Linkedin, so people can just click a button and use their Linkedin account to login, just like on Facebook or Twitter? Both use OAuth, but I found specified libraries for them which are dead simple to use. For Linkedin I only found some sample code in DotNetOpenAuth but I can't make any sense out of it.

是否有任何图书馆,我可以用它来方便的登录功能,LinkedIn?或者有什么如何做,在ASP.NET MVC与DotNetOpenAuth 4教程?

Are there any libraries I can use to facilitate a login function for Linkedin? Or any tutorials on how to do that in ASP.NET MVC with DotNetOpenAuth 4?

推荐答案

下面就是看起来是一个pretty的固体样品

Here's what looks to be a pretty solid sample

HTTP ://mrsarker.word$p$pss.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

}

这篇关于与LinkedIn登陆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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