Joh*_* P. 6 c# url styles webclient process
我有一个在服务器上验证我的凭据的URL.有没有办法让它看不见?简单的代码看起来完全像这样:
public void DoAuth()
{
String uri = GetUri();
ProcessStartInfo startInfo = new ProcessStartInfo(uri);
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
Process.Start(startInfo);
}
Run Code Online (Sandbox Code Playgroud)
但ProcessWindowStyle.Hidden似乎没有做到这一点.如果我设置UseShellExecute为false然后我收到Win32Exception消息The system cannot find the file specified
该url是Spotify服务器上的身份验证以获取播放列表,它看起来像这样https://accounts.spotify.com/authorize/client_id=26d287105as12315e12ds56e31491889f3cd293..
还有其他方法可以使这个过程不可见吗?
编辑:http示例
public void DoAuth()
{
String uri = GetUri();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
HttpWebResponse webResponse;
try
{
webResponse = (HttpWebResponse)request.GetResponse();
Console.WriteLine("Error code: {0}", webResponse.StatusCode);
using (Stream data = webResponse.GetResponseStream())
using (var reader = new StreamReader(data))
{
//do what here?
}
}
catch (Exception e)
{
throw;
}
}
Run Code Online (Sandbox Code Playgroud)
包含上述示例的整个.cs文件:
using SpotifyAPI.Web.Enums;
using SpotifyAPI.Web.Models;
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Threading;
namespace SpotifyAPI.Web.Auth
{
public class ImplicitGrantAuth
{
public delegate void OnResponseReceived(Token token, String state);
private SimpleHttpServer _httpServer;
private Thread _httpThread;
public String ClientId { get; set; }
public String RedirectUri { get; set; }
public String State { get; set; }
public Scope Scope { get; set; }
public Boolean ShowDialog { get; set; }
public event OnResponseReceived OnResponseReceivedEvent;
/// <summary>
/// Start the auth process (Make sure the internal HTTP-Server ist started)
/// </summary>
public void DoAuth()
{
String uri = GetUri();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
HttpWebResponse webResponse;
try
{
webResponse = (HttpWebResponse)request.GetResponse();
Console.WriteLine("Error code: {0}", webResponse.StatusCode);
using (Stream data = webResponse.GetResponseStream())
using (var reader = new StreamReader(data))
{
//nothing
}
}
catch (Exception e)
{
throw;
}
/*ProcessStartInfo startInfo = new ProcessStartInfo(uri);
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
Process.Start(startInfo);
*/
}
private String GetUri()
{
StringBuilder builder = new StringBuilder("https://accounts.spotify.com/authorize/?");
builder.Append("client_id=" + ClientId);
builder.Append("&response_type=token");
builder.Append("&redirect_uri=" + RedirectUri);
builder.Append("&state=" + State);
builder.Append("&scope=" + Scope.GetStringAttribute(" "));
builder.Append("&show_dialog=" + ShowDialog);
return builder.ToString();
}
/// <summary>
/// Start the internal HTTP-Server
/// </summary>
public void StartHttpServer(int port = 80)
{
_httpServer = new SimpleHttpServer(port, AuthType.Implicit);
_httpServer.OnAuth += HttpServerOnOnAuth;
_httpThread = new Thread(_httpServer.Listen);
_httpThread.Start();
}
private void HttpServerOnOnAuth(AuthEventArgs e)
{
OnResponseReceivedEvent?.Invoke(new Token
{
AccessToken = e.Code,
TokenType = e.TokenType,
ExpiresIn = e.ExpiresIn,
Error = e.Error
}, e.State);
}
/// <summary>
/// This will stop the internal HTTP-Server (Should be called after you got the Token)
/// </summary>
public void StopHttpServer()
{
_httpServer.Dispose();
_httpServer = null;
}
}
}
Run Code Online (Sandbox Code Playgroud)
它们被称为这样:
_auth.OnResponseReceivedEvent += _auth_OnResponseReceivedEvent;
_auth.StartHttpServer(8000);
_auth.DoAuth();
Run Code Online (Sandbox Code Playgroud)
带有完整可运行样品的github url在这里:https://github.com/JohnnyCrazy/SpotifyAPI-NET 下载并运行Spotify测试以连接Spotify的web api以重现我的样本.
经典的 XY 问题,您不是在寻找隐藏窗口的方法,您想要在不涉及用户操作的情况下对用户进行身份验证。
由于ImplicitGrantAuth不适用于简单的 HTTP 请求,因此您需要一个解决方法:
虽然ImplicitGrantAuth不是为此类任务而设计的,但它仍然是可能的,但并不干净。您将需要一个可以在 C# 应用程序中控制的无头浏览器,例如Selenium。
考虑使用 Selenium 的以下代码(相关的 SO 问题):
void DoAuth()
{
IWebDriver driver = new FirefoxDriver();
driver.Navigate().GoToUrl(GetUri());
IWebElement query = driver.FindElement(By.Name("username")); //You need to search for the correct element
query.SendKeys("username");
query = driver.FineElement(By.Name("password"));
query.SendKeys("password");
//Somehow submit the form and wait till you get the Token via URL
//Thread.Sleep(5000);
String urlWithToken = driver.Url;
//Analyze it and get the token out of it
driver.Quit();
}
Run Code Online (Sandbox Code Playgroud)
这只是伪正确的代码,但您应该明白这一点。使用您可以控制的无头浏览器,填写所有表单输入,提交并分析 URL。Facebook 登录也是同样的原理。
但请注意: OAuth 不是为这种类型的使用而设计的,您可能不应该在生产中使用这种黑客的解决方法