Files
KJD_WebService/TEMUKJD/KJDForms/ToolForms/PinDuoDuoLoginForm.cs
T

191 lines
6.6 KiB
C#

using AllBase;
using CefSharp;
using CefSharp.WinForms;
using TemuKJD.KJDForms.BaseForms;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace TemuKJD.KJDForms.ToolForms
{
public partial class PinDuoDuoLoginForm : LRBaseForm
{
private ChromiumWebBrowser cefWebBrowser = null;
public static string LoginUrl = "http://yangkeduo.com/login.html";
public static string MobileLoginUrl = BaseClass.NewUrl+"/login.html";
public delegate void OnSaveComplete(string msg);
public event OnSaveComplete onSaveComplete;
public PinDuoDuoLoginForm()
{
InitializeComponent();
}
private void PinDuoDuoLoginForm_Load(object sender, EventArgs e)
{
if (BaseClass.Ico != null)
Icon = BaseClass.Ico;
cefWebBrowser = CreateChromiumWebBrowser(MobileLoginUrl,false);
cefWebBrowser.FrameLoadEnd += CefWebBrowser_FrameLoadEnd;
cefWebBrowser.Dock = DockStyle.Fill;
panel1.Controls.Add(cefWebBrowser);
}
private void RequestHandler_ResourceLoadComplete(string url, string postData, string responseData)
{
}
public static RequestContextSettings GetRequestContextIsolateSettings()
{
RequestContextSettings requestContextSettings = new RequestContextSettings();
requestContextSettings.PersistSessionCookies = false;
//cef138//requestContextSettings.PersistUserPreferences = false;
return requestContextSettings;
}
public static ChromiumWebBrowser CreateChromiumWebBrowser(string url, bool isGlobal = true)
{
ChromiumWebBrowser cefBrowser = new ChromiumWebBrowser(url);
if (!isGlobal)
{
cefBrowser.RequestContext = new RequestContext(GetRequestContextIsolateSettings());
}
cefBrowser.BrowserSettings = new BrowserSettings
{
/*Plugins = CefState.Disabled*/ /*cef92*/
};
return cefBrowser;
}
public static List<Cookie> cookieList = new List<Cookie>();
private async void CefWebBrowser_FrameLoadEnd(object sender, CefSharp.FrameLoadEndEventArgs e)
{
if (e.Frame.IsMain)
{
string url = e.Frame.Url;
string html = await e.Frame.GetSourceAsync();
List<Cookie> cookieList = GetCookieList("yangkeduo.com", cefWebBrowser.RequestContext.GetCookieManager(null));
string token = cookieList.Where(p => p.Name == "PDDAccessToken").Select(p => p.Value).FirstOrDefault();
if (!string.IsNullOrEmpty(token))
{
this.BeginInvoke(new Action(() =>
{
this.onSaveComplete(token);
this.Close();
}));
}
}
//string url = cefWebBrowser.Address;
//if (url.Contains(BaseClass.NewUrl+"/index.html"))
//{
// List<Cookie> cookieList = GetCookieList("yangkeduo.com", null);
// string token = cookieList.Where(p => p.Name == "PDDAccessToken").Select(p => p.Value).FirstOrDefault();
// if (!string.IsNullOrEmpty(token))
// {
// this.BeginInvoke(new Action(() =>
// {
// this.onSaveComplete(token);
// this.Close();
// }));
// }
//}
}
public static List<Cookie> GetCookieList(string domains = "", ICookieManager cookieManager = null)
{
if (cookieManager == null)
{
cookieManager = Cef.GetGlobalCookieManager();
}
CookieVisitorx visitor = new CookieVisitorx();
if (cookieManager.VisitAllCookies(visitor))
{
visitor.WaitForAllCookie();
}
if (string.IsNullOrEmpty(domains))
{
return visitor.CookieList;
}
else
{
List<Cookie> list = new List<Cookie>();
foreach (string domain in domains.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries))
{
list.AddRange(visitor.CookieList.Where(p => p.Domain.Contains(domain.Trim())).ToList());
}
return list;
}
}
}
public class CookieVisitorx : ICookieVisitor
{
public List<Cookie> CookieList;
public ManualResetEvent gotAllCookie = new ManualResetEvent(false);
public CookieVisitorx()
{
CookieList = new List<Cookie>();
}
public bool Visit(Cookie cookie, int count, int total, ref bool deleteCookie)
{
CookieList.Add(cookie);
if (count == total - 1)
{
gotAllCookie.Set();
}
return true;
}
public void WaitForAllCookie()
{
gotAllCookie.WaitOne();
}
#region IDisposable Support
private bool disposedValue = false; // To detect redundant calls
protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing)
{
// TODO: dispose managed state (managed objects).
}
// TODO: free unmanaged resources (unmanaged objects) and override a finalizer below.
// TODO: set large fields to null.
disposedValue = true;
}
}
// TODO: override a finalizer only if Dispose(bool disposing) above has code to free unmanaged resources.
// ~CookieVisitor()
// {
// // Do not change this code. Put cleanup code in Dispose(bool disposing) above.
// Dispose(false);
// }
// This code added to correctly implement the disposable pattern.
void IDisposable.Dispose()
{
// Do not change this code. Put cleanup code in Dispose(bool disposing) above.
Dispose(true);
// TODO: uncomment the following line if the finalizer is overridden above.
// GC.SuppressFinalize(this);
}
#endregion
}
}