所以我是C#的新手,我不知道我看到的是不是明显的有意为之的行为,或者我正在使用的对象是否有一些细微差别.
我正在try 设置一个服务器来处理StackOverflow应用程序的OAuth流的重定向.构建的重定向URL看起来像localhost:8080/#access_token=12345
.
我基本上将以下内容设置为我的简单服务器,以处理重定向请求并保存access_token
.
public class ImplicitFlowHttpServer
{
public int Port = 8080;
private HttpListener _listener;
public void Start()
{
var prefix = $"http://*:8080/";
_listener = new HttpListener();
_listener.Prefixes.Add(prefix);
_listener.Start();
_listener.BeginGetContext(new AsyncCallback(ListenerCallback), _listener);
}
private async void ListenerCallback(IAsyncResult result)
{
if (_listener.IsListening)
{
string oauthCode;
var context = _listener.EndGetContext(result);
var request = context.Request;
// code to send response, not relevant to my question
HttpListenerResponse response = context.Response;
byte[] page = Encoding.ASCII.GetBytes(
"You can close this browser tab now.");
response.ContentLength64 = page.Length;
Stream output = response.OutputStream;
output.Write(page, 0, page.Length);
output.Close();
//
Console.WriteLine($"Should receive a request to localhost:8080/#access_token=<token>");
Console.WriteLine($"How can I see the access_token?");
Console.WriteLine($"request.Url: {request.Url}");
Console.WriteLine($"request.Url.Fragment: {request.Url.Fragment}");
Console.WriteLine($"request.RawUrl: {request.RawUrl}");
Console.WriteLine($"request.Url.AbsoluteUri: {request.Url.AbsoluteUri}");
Console.WriteLine($"request.Url.OriginalString: {request.Url.OriginalString}");
// <here is code to parse the access_token if it existed>
// <here is code to stop the server>
}
}
}
你可以看到,我期待的是request
对象中的某个地方的散列,#access_token
会存在,我只需要弄清楚如何将其取出,但我认为它已经完全被清除了.
这与重定向无关,因为如果我启动服务器然后执行curl "localhost:8080/?q=123&hello=world\#access_token=123456"
,我也会遇到同样的问题
有了上面的代码,我将看到
request.Url: http://localhost:8080/?q=123&hello=world%5C
request.Url.Fragment:
request.RawUrl: /?q=123&hello=world\
request.Url.AbsoluteUri: http://localhost:8080/?q=123&hello=world%5C
request.Url.OriginalString: http://localhost:8080/?q=123&hello=world%5C
因此,鉴于我被这个访问令牌"in the hash"困住了,我应该如何从那个重定向中获得access_token
呢?