我有一个网址,用于在服务器上验证我的凭据。有没有办法使它不可见?简单的代码如下所示:
public void DoAuth()
{
String uri = GetUri();
ProcessStartInfo startInfo = new ProcessStartInfo(uri);
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
Process.Start(startInfo);
}
但是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;
}
}
包含上述示例的整个.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;
}
}
}
他们被这样称呼:
_auth.OnResponseReceivedEvent += _auth_OnResponseReceivedEvent;
_auth.StartHttpServer(8000);
_auth.DoAuth();
带有完整可运行样本的github URL在这里:https : //github.com/JohnnyCrazy/SpotifyAPI-NET下载它并运行Spotify测试以连接到Spotify的Web api以重现我的示例。
经典XY问题,您不是要寻找隐藏窗口的方法,而是要在不涉及用户操作的情况下对用户进行身份验证。
由于ImplicitGrantAuth
不适用于简单的HTTP请求,因此您需要一种解决方法:
尽管ImplicitGrantAuth
不是为完成这种任务而做的,但仍然有可能,但并非一帆风顺。您将需要一个无头的浏览器,可以在C#App中对其进行控制,例如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();
}
这只是伪正确的代码,但是您应该明白这一点。使用无头浏览器,您可以控制,填写所有表单输入,提交和分析URL。Facebook登录将是相同的原则。
但请注意: OAuth并非针对此类使用而设计,因此您不应该在生产中使用这种棘手的变通办法
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句