asp.net-mvc-4 – LinkedIn在MVC4中使用DotNetOpenAuth的完整个
发布时间:2020-12-16 04:04:43 所属栏目:asp.Net 来源:网络整理
导读:我的MVC4应用程序允许使用LinkedIn帐户登录.我想从登录用户的linkedIn中提取所有可用的详细信息.目前我做了以下工作. 在My AuthConfig.cs中, Dictionarystring,object linkedInExtraData = new Dictionarystring,object(); linkedInExtraData.Add("Icon","..
我的MVC4应用程序允许使用LinkedIn帐户登录.我想从登录用户的linkedIn中提取所有可用的详细信息.目前我做了以下工作.
在My AuthConfig.cs中, Dictionary<string,object> linkedInExtraData = new Dictionary<string,object>(); linkedInExtraData.Add("Icon","../Images/linkedIn.png"); OAuthWebSecurity.RegisterClient( client: new App_Start.LinkedInCustomClient("xxxxxxxxxxxx","yyyyyyyyyyyyyyy"),displayName: "LinkedIn",extraData: linkedInExtraData); 在LinkedIn Developer Kit中的linkedInCustomClient.cs中 public class LinkedInCustomClient : OAuthClient { private static XDocument LoadXDocumentFromStream(Stream stream) { var settings = new XmlReaderSettings { MaxCharactersInDocument = 65536L }; return XDocument.Load(XmlReader.Create(stream,settings)); } /// Describes the OAuth service provider endpoints for LinkedIn. private static readonly ServiceProviderDescription LinkedInServiceDescription = new ServiceProviderDescription { AccessTokenEndpoint = new MessageReceivingEndpoint("https://api.linkedin.com/uas/oauth/accessToken",HttpDeliveryMethods.PostRequest),RequestTokenEndpoint = new MessageReceivingEndpoint("https://api.linkedin.com/uas/oauth/requestToken?scope=r_fullprofile",UserAuthorizationEndpoint = new MessageReceivingEndpoint("https://www.linkedin.com/uas/oauth/authorize",TamperProtectionElements = new ITamperProtectionChannelBindingElement[] { new HmacSha1SigningBindingElement() },ProtocolVersion = ProtocolVersion.V10a }; public LinkedInCustomClient(string consumerKey,string consumerSecret) : base("linkedIn",LinkedInServiceDescription,consumerKey,consumerSecret) { } /// Check if authentication succeeded after user is redirected back from the service provider. /// The response token returned from service provider authentication result. [SuppressMessage("Microsoft.Design","CA1031:DoNotCatchGeneralExceptionTypes",Justification = "We don't care if the request fails.")] protected override AuthenticationResult VerifyAuthenticationCore(AuthorizedTokenResponse response) { // See here for Field Selectors API http://developer.linkedin.com/docs/DOC-1014 const string profileRequestUrl = "https://api.linkedin.com/v1/people/~:(id,first-name,last-name,interests,headline,industry,summary,email-address,location:(name),picture-url,positions,associations,languages,honors,educations,date-of-birth,primary-twitter-account,three-current-positions,three-past-positions,group-memberships,specialties,skills)"; string accessToken = response.AccessToken; string tokenSecret = (response as ITokenSecretContainingMessage).TokenSecret; string Verifier = response.ExtraData.Values.First(); var profileEndpoint = new MessageReceivingEndpoint(profileRequestUrl,HttpDeliveryMethods.GetRequest); HttpWebRequest request = WebWorker.PrepareAuthorizedRequest(profileEndpoint,accessToken); try { using (WebResponse profileResponse = request.GetResponse()) { using (Stream responseStream = profileResponse.GetResponseStream()) { XDocument document = LoadXDocumentFromStream(responseStream); return new AuthenticationResult( isSuccessful: true,provider: ProviderName,providerUserId: userId,userName: userName,extraData: extraData); } } } catch (Exception exception) { return new AuthenticationResult(exception); } } } 在我的控制器中, AuthenticationResult result = OAuthWebSecurity.VerifyAuthentication(Url.Action("ExternalLoginCallback",new { ReturnUrl = returnUrl })); if (!result.IsSuccessful) { return RedirectToAction("ExternalLoginFailure"); } 我需要在控制器中获取以下详细信息作为身份验证结果. (id,skills) 解决方法
来自LinkedIn的请求的响应将是一个xml文件.格式和字段在
LinkedIn Profile Fields中提到
要获取电子邮件字段,您需要将请求令牌网址修改为 RequestTokenEndpoint = new MessageReceivingEndpoint(“https://api.linkedin.com/uas/oauth/requestToken?scope=r_fullprofile r_emailaddress”, 您可以按照以下代码中的要求获取字段 XDocument document = LoadXDocumentFromStream(responseStream); 例如:要从xml文件中获取名字字段, var firstName = document.Root.Element("first-name").Value; 语言,职位,技能等字段将作为结构对象返回,作为配置文件的一部分. 例如:语言领域. var Lang = document.Root.Element("languages"); var languages = new List<string>(); if (Lang != null) { foreach (var l in Lang.Elements()) { if (l.Element("language") != null && l.Element("language").Element("name") != null) { languages.Add(l.Element("language").Element("name").Value); } } } 然后,您可以将字段添加到“extraData”,可以在控制器中访问. extraData.Add("firstName",firstName); extraData.Add("languages",lang); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc – 在ASP.NET MVC应用程序中放置数据操作和业务
- asp.net的ftp的上传和下载
- asp.net-mvc – Elmah.MVC对Elmah.contrib.Mvc
- asp.net-mvc – 输出字符串里的字符串文字引用
- asp.net – 未指定SMTP主机
- 使用System.Net.Mail中的SMTP发送邮件(带附件)
- asp.net-mvc – 文件“?/ Views/Position/Edit.cshtml”不能
- ASP.NET WebApi RESTful标准
- ASP.NET Core 企业开发架构概述
- asp.net – 在IIS上部署MVC应用程序时,我收到一个空白页面
推荐文章
站长推荐
- asp.net-web-api – Web API中的OData POST的媒体
- asp.net-mvc – 用数据测试ASP.NET MVC的单元
- asp.net – 类型存在于’A’和’B’
- asp-classic – 在VBScript中输出GUID会忽略它之
- asp.net-mvc-4 – DotLess @import
- asp.net-web-api – 找不到Web API路由404
- 构建ASP.NET应用程序 – 最佳实践
- ASP.net MVC路由:使用QueryStrings是一种好的风
- asp.net – 如何查询具有多个根和过滤的嵌套集模
- asp.net-web-api – 返回Web API中的错误
热点阅读