public RequestSession Login(WebProxy proxy = null) { string address = proxy != null ? proxy.Address.ToString() : ""; Console.WriteLine("登出LinkedIn...Proxy: {0}", address); var session = new RequestSession(this); string result; HttpRequestContext requestContext = new HttpRequestContext(this.LinkedInWebSystemConfig.MainEntryUrl) { Referer = LinkedInWebSystemConfig.MainEntryUrl, KeepAlive = true, TimeOut = 10000 }; HttpRequestUtils.RequestHtmlPage(requestContext, out result, proxy); session.CookieContainer = requestContext.CookieContainer; result = string.Empty; HttpRequestContext loginRequestContext = new HttpRequestContext(this.LinkedInWebSystemConfig.LoginUrl) { Referer = LinkedInWebSystemConfig.MainEntryUrl, KeepAlive = true, TimeOut = 10000 }; HttpRequestUtils.RequestHtmlPage(loginRequestContext, out result, proxy); session.CookieContainer = loginRequestContext.CookieContainer; string csrfToken = string.Empty; string sourceAlias = string.Empty; csrfToken = this.GetCSRFToken(result); sourceAlias = this.GetSourceAlias(result); // string viewStateValue = string.Empty; //viewStateValue = this.GetViewState(result); Console.WriteLine("登录LinkedIn...Proxy: {0}", address); string postData = String.Format(this.LinkedInWebSystemConfig.LoginPostDataFormat, HttpUtility.UrlEncode(this.LinkedInWebSystemConfig.LoginParameters["UserName"].Value), HttpUtility.UrlEncode(this.LinkedInWebSystemConfig.LoginParameters["Password"].Value), csrfToken, sourceAlias); HttpPostRequestContext postRequestContext = new HttpPostRequestContext(this.LinkedInWebSystemConfig.LoginSubmitUrl, postData) { CookieContainer = session.CookieContainer, Referer = LinkedInWebSystemConfig.MainEntryUrl, KeepAlive = true, TimeOut = 2000 }; string loginResult; HttpRequestUtils.RequestPostHtmlPage(postRequestContext, out loginResult, proxy); Console.WriteLine("登录LinkedIn成功....Proxy: {0}", address); session.CookieContainer = postRequestContext.CookieContainer; return session; }
public bool Login(bool saveSession, out string message) { Console.WriteLine("登出LinkedIn..."); this.CurrentSession = new RequestSession(this); string result; message = ""; HttpRequestContext requestContext = new HttpRequestContext(this.LinkedInWebSystemConfig.MainEntryUrl) { Referer = LinkedInWebSystemConfig.MainEntryUrl, KeepAlive = true, TimeOut = 50000 }; HttpRequestUtils.RequestHtmlPage(requestContext, out result); CurrentSession.CookieContainer = requestContext.CookieContainer; result = string.Empty; HttpRequestContext loginRequestContext = new HttpRequestContext(this.LinkedInWebSystemConfig.LoginUrl) { Referer = LinkedInWebSystemConfig.MainEntryUrl, KeepAlive = true, TimeOut = 50000 }; HttpRequestUtils.RequestHtmlPage(loginRequestContext, out result); CurrentSession.CookieContainer = loginRequestContext.CookieContainer; string csrfToken = string.Empty; string sourceAlias = string.Empty; csrfToken = this.GetCSRFToken(result); sourceAlias = this.GetSourceAlias(result); // string viewStateValue = string.Empty; //viewStateValue = this.GetViewState(result); Console.WriteLine("登陆LinkedIn..."); string postData = String.Format(this.LinkedInWebSystemConfig.LoginPostDataFormat, HttpUtility.UrlEncode(this.LinkedInWebSystemConfig.LoginParameters["UserName"].Value), HttpUtility.UrlEncode(this.LinkedInWebSystemConfig.LoginParameters["Password"].Value), csrfToken, sourceAlias); HttpPostRequestContext postRequestContext = new HttpPostRequestContext(this.LinkedInWebSystemConfig.LoginSubmitUrl, postData) { CookieContainer = CurrentSession.CookieContainer, Referer = LinkedInWebSystemConfig.MainEntryUrl, KeepAlive = true, TimeOut = 50000 }; string loginResult; HttpRequestUtils.RequestPostHtmlPage(postRequestContext, out loginResult); return true; }
protected async override Task ApplyResponseGrantAsync() { var signout = this.Helper.LookupSignOut(this.Options.AuthenticationType, this.Options.AuthenticationMode); if (signout == null) { return; } try { this._logger.WriteInformation(String.Format("Applying response grand for authenticationType: {0}, authenticationMode: {1}. Path: {2}", this.Options.AuthenticationType, this.Options.AuthenticationMode, this.Request.Path)); var logoutContextBuilder = this._resolver.Resolve <ISamlLogoutContextResolver <IOwinRequest> >(); var logoutContext = logoutContextBuilder.ResolveLogoutContext(Request); var federationPartyId = logoutContext.FederationPartyId; var configurationManager = this._resolver.Resolve <IConfigurationManager <MetadataBase> >(); var configuration = await configurationManager.GetConfigurationAsync(federationPartyId, new CancellationToken()); if (configuration == null) { throw new InvalidOperationException("Cannot obtain metadata."); } var metadataType = configuration.GetType(); var handlerType = typeof(IMetadataHandler <>).MakeGenericType(metadataType); var handler = this._resolver.Resolve(handlerType) as IMetadataHandler; if (handler == null) { throw new InvalidOperationException(String.Format("Handler must implement: {0}", typeof(IMetadataHandler).Name)); } var idp = handler.GetIdentityProviderSingleSignOnDescriptor(configuration) .Single().Roles.Single(); var federationPartyContextBuilder = this._resolver.Resolve <IAssertionPartyContextBuilder>(); var federationContext = federationPartyContextBuilder.BuildContext(federationPartyId); var signoutUrl = handler.GetIdentityProviderSingleLogoutService(idp, federationContext.OutboundBinding); var requestContext = new OwinLogoutRequestContext(Context, signoutUrl, base.Request.Uri, federationContext, logoutContext); var relayStateAppenders = this._resolver.ResolveAll <IRelayStateAppender>(); foreach (var appender in relayStateAppenders) { await appender.BuildRelayState(requestContext); } SamlOutboundContext outboundContext = null; if (federationContext.OutboundBinding == new Uri(Bindings.Http_Redirect)) { outboundContext = new HttpRedirectRequestContext { BindingContext = new RequestBindingContext(requestContext), DespatchDelegate = redirectUri => { this._logger.WriteInformation(String.Format("Redirecting to:\r\n{0}", redirectUri.AbsoluteUri)); this.Response.Redirect(redirectUri.AbsoluteUri); return(Task.CompletedTask); } }; } else { outboundContext = new HttpPostRequestContext(new SAMLForm()) { BindingContext = new RequestPostBindingContext(requestContext), DespatchDelegate = (form) => { this._logger.WriteInformation(String.Format("Writing saml form to the response.")); Response.Write(form.ToString()); return(Task.CompletedTask); }, }; } var protocolContext = new SamlProtocolContext { RequestContext = outboundContext }; var protocolFactory = this._resolver.Resolve <Func <string, IProtocolHandler> >(); var protocolHanlder = protocolFactory(federationContext.OutboundBinding.AbsoluteUri); await protocolHanlder.HandleOutbound(protocolContext); } catch (Exception ex) { this._logger.WriteError("An exception has been thrown when applying challenge", ex); throw; } }
public void GetProfileIds() { if (!Login()) { return; } var configPath = this.GetConfigurationPath("ProfileIdSearch"); var postalCodeConfig = ProviderConfiguration.GetConfigurationEntity<PostalCodeConfiguration>(configPath); var storedDirPath = string.Format(@"{0}\{1}", Environment.CurrentDirectory, "LinkedInSearchResult"); if (!Directory.Exists(storedDirPath)) { Directory.CreateDirectory(storedDirPath); } #region search code int totalSearchCount = 1; int cityCount = 0; int errorCount = 0; // Parallel.ForEach(this.PostalCodeConfiguration.StateCollection, (state,loopState) => Array.ForEach(postalCodeConfig.StateCollection.ToArray(), (state) => { //if (loopState.IsStopped) //{ // loopState.Stop(); // return; //} // Parallel.ForEach(state.PostalCodeCollection, (city,loopStateCity) => Array.ForEach(state.PostalCodeCollection.ToArray(), (city) => { if (city.Checked) { return; } if (cityCount >= this.LinkedInWebSystemConfig.LoginConfiguration.PostCodeCount) { return; } cityCount++; //if (loopStateCity.IsStopped) //{ // loopStateCity.Stop(); // return; //} if (totalSearchCount >= 1300 || errorCount >= 10) { // loopStateCity.Stop(); return; } #region Search Advanced Initialization /* string orig = ""; string rsid = ""; int tryCount = 1; tryCount = 1; while (tryCount++ <= 2) { try { Console.WriteLine("LinkedIn Advance Search Initailizaition , City:{0}, PostalCode: {1}...", city.Name, city.Code); HttpRequestContext requestContext = new HttpRequestContext(this.LinkedInWebSystemConfig.AdvanceProfileUrl) { CookieContainer = this.CurrentSession.CookieContainer, Referer = LinkedInWebSystemConfig.MainEntryUrl, KeepAlive = true, TimeOut = 15000 }; HttpRequestUtils.RequestHtmlPage(requestContext, out result); orig = GetOrIg(result); rsid = GetRsId(result); // Thread.Sleep(30000); break; } catch (WebException ex) { errorCount++; Console.WriteLine("获取数据失败,Error:{0}", ex.Message); if (ex.Response != null) { var response = ex.Response as HttpWebResponse; if (response != null) { if (response.StatusCode == HttpStatusCode.Found) { errorCount = 50; } } } break; } catch (Exception ex) { errorCount++; Console.WriteLine("LinkedIn Advance Search Initailizaition , City:{0}, PostalCode: {1}, Error:{2}", city.Name, city.Code, ex.Message); } } */ #endregion int searchIndex = 1; int webErrorCount = 0; ErrorType errorType = ErrorType.None; #region main search code while (searchIndex <= 70) { totalSearchCount++; if (errorType == ErrorType.NonResult || errorType == ErrorType.NotFullResult) { break; } int tryCount = 1; while (tryCount++ <= 2) { try { string result = string.Empty; Console.WriteLine("LinkedIn Advance Search Result , City:{0}, PostalCode: {1},页码:{2}...", city.Name, city.Code, searchIndex); string searchPostData = string.Format(this.LinkedInWebSystemConfig.SearchPostDataFormat, city.Code, searchIndex); HttpPostRequestContext requestContext = new HttpPostRequestContext(this.LinkedInWebSystemConfig.SearchProfileUrl, searchPostData) { CookieContainer = this.CurrentSession.CookieContainer, Referer = LinkedInWebSystemConfig.MainEntryUrl, KeepAlive = true, TimeOut = 20000 }; HttpRequestUtils.RequestPostHtmlPage(requestContext, out result); var fileName = city.Code + "_" + searchIndex; errorType = LinkedInAnalyzer.CheckResponseForProfileId(result); Thread.Sleep(5000); if (errorType == ErrorType.NonResult) { break; } File.WriteAllText(string.Format(@"{0}\{1}\{2}.txt", Environment.CurrentDirectory, "LinkedInSearchResult", fileName), result); break; } catch (WebException ex) { errorCount++; webErrorCount++; Console.WriteLine("获取数据失败,Error:{0}", ex.Message); if (ex.Response != null) { var response = ex.Response as HttpWebResponse; if (response != null) { if (response.StatusCode == HttpStatusCode.Found) { errorType = ErrorType.NonResult; totalSearchCount = 1000; webErrorCount = 10; } } } break; } catch (Exception ex) { errorCount++; Console.WriteLine("LinkedIn Advance Search Result Exception , City:{0}, PostalCode: {1}, Error:{2},页码:{2}...", city.Name, city.Code, ex.Message, searchIndex); } } searchIndex++; } #endregion if (webErrorCount < 10) { city.Checked = true; } }); }); #endregion int tr2yCount = 0; while (tr2yCount++ <= 2) { try { ProviderConfiguration.SerializeConfigurationEntityToXml<PostalCodeConfiguration>(postalCodeConfig, configPath,true); break; } catch (Exception ex) { } } Console.WriteLine("搜索结束了。。。。"); }
public async Task Post_end_to_end_test() { //ARRANGE var isValid = false; string url = String.Empty; IDictionary <string, object> relayState = null; var builders = new List <IPostClauseBuilder>(); var requestUri = new Uri("http://localhost:59611/"); var federationPartyContextBuilder = new FederationPartyContextBuilderMock(); var federationContex = federationPartyContextBuilder.BuildContext("local"); var spDescriptor = federationContex.MetadataContext.EntityDesriptorConfiguration.SPSSODescriptors.First(); var certContext = spDescriptor.KeyDescriptors.Where(x => x.Use == KeyUsage.Signing && x.IsDefault) .Select(x => x.CertificateContext) .First(); var supportedNameIdentifierFormats = new List <Uri> { new Uri(NameIdentifierFormats.Transient) }; var authnRequestContext = new AuthnRequestContext(requestUri, new Uri("http://localhost"), federationContex, supportedNameIdentifierFormats); authnRequestContext.RelyingState.Add("relayState", "Test state"); var xmlSerialiser = new XMLSerialiser(); var compressor = new DeflateCompressor(); var encoder = new MessageEncoding(compressor); var logger = new LogProviderMock(); var serialiser = new RequestSerialiser(xmlSerialiser, encoder, logger); RequestHelper.GetAuthnRequestBuilders = AuthnRequestBuildersFactoryMock.GetAuthnRequestBuildersFactory(); var authnBuilder = new SamlRequestBuilder(serialiser); builders.Add(authnBuilder); //relay state builder var jsonSerialiser = new NSJsonSerializer(new DefaultSettingsProvider()); var relayStateSerialiser = new RelaystateSerialiser(jsonSerialiser, encoder, logger) as IRelayStateSerialiser; var relayStateBuilder = new RelayStateBuilder(relayStateSerialiser); builders.Add(relayStateBuilder); //signature builder var certificateManager = new CertificateManager(logger); var xmlSinatureManager = new XmlSignatureManager(); var signatureBuilder = new SignatureBuilder(certificateManager, logger, xmlSinatureManager); builders.Add(signatureBuilder); //context var outboundContext = new HttpPostRequestContext(new SAMLForm()) { BindingContext = new RequestPostBindingContext(authnRequestContext), DespatchDelegate = form => { url = form.ActionURL; var request = ((SAMLForm)form).HiddenControls[HttpRedirectBindingConstants.SamlRequest]; var state = ((SAMLForm)form).HiddenControls[HttpRedirectBindingConstants.RelayState]; var task = relayStateSerialiser.Deserialize(state); task.Wait(); relayState = task.Result as IDictionary <string, object>; var cert = certificateManager.GetCertificateFromContext(certContext); isValid = this.VerifySignature(request, cert); return(Task.CompletedTask); } }; //dispatcher var dispatcher = new PostRequestDispatcher(() => builders, logger); //ACT await dispatcher.SendAsync(outboundContext); //ASSERT Assert.AreEqual(url, requestUri.AbsoluteUri); Assert.IsTrue(Enumerable.SequenceEqual(relayState, authnRequestContext.RelyingState)); Assert.IsTrue(isValid); }