如何实现Linkedin的登录方法,人们只需点击一个按钮并使用他们的Linkedin帐户登录,就像在Facebook或Twitter上一样?两者都使用OAuth,但我发现它们的指定库很容易使用.对于Linkedin我只在DotNetopenAuth中找到了一些示例代码,但我无法理解它.
是否有任何库可用于促进Linkedin的登录功能?或者有关如何使用DotNetopenAuth 4在ASP.NET MVC中执行此操作的任何教程?
解决方法
这看起来是一个非常可靠的样本
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"],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,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 }