.net – 用Linkedin登录
发布时间:2020-12-16 03:52:20 所属栏目:asp.Net 来源:网络整理
导读:如何实现Linkedin的登录方法,人们只需点击一个按钮并使用他们的Linkedin帐户登录,就像在Facebook或Twitter上一样?两者都使用OAuth,但我发现它们的指定库很容易使用.对于Linkedin我只在DotNetOpenAuth中找到了一些示例代码,但我无法理解它. 是否有任何库可用
如何实现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 } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- ASP.net MVC Webforms视图引擎的缺点?
- asp.net-web-api – ASP.NET Core中的IHttpActionResult和帮
- 从ASP.Net页面运行批处理文件
- asp.net-core – 丰富Serlilogs,每个hangfire工作具有独特的
- asp.net-mvc-3 – 不再使用XXXX.现在用于检测模型中的更改
- asp.net下Cache 缓存操作类代码
- asp.net – 关系从一对多变为多对多需要更新listview
- asp.net-mvc-3 – 使用jQuery验证货币字段的客户端验证
- asp.net-mvc – 与ASP.NET MVC集成的经典ASP
- asp.net-mvc – MVC错误 – 传入字典的模型项目的类型为“S
推荐文章
站长推荐
- asp.net-mvc – 存储库层中的服务层重复功能
- asp.net – 从GridView中的2列动态生成超链接
- asp-classic – ASP Classic中的溢出
- asp.net-mvc – HtmlHelper NameFor方法
- asp.net – 如何配置IIS Express来调试子目录中的
- asp.net-mvc – 检查上传的文件是否是C#ASP.NET
- asp.net-mvc – 从IIS重写http到https的URL重写不
- asp.net – 为什么我无法访问usercontrol中的页面
- asp.net-mvc-5 – 如何添加声明在ASP.NET身份
- asp.net-mvc – “无法同时分析32位和64位应用程
热点阅读