当 url 包含 # 时,Getting full url 不起作用

Getting full url not working when url contains #

你好堆栈溢出窥视,

在实施 oAuth 系统时遇到一些问题。

//https://github.com/justintv/Twitch-API/blob/master/authentication.md
public ActionResult AuthorizeTwitch(CancellationToken cancellationToken) {
    var twitchBridge = new TwitchBridge();
    var codes = Request.Params.GetValues("code");
    var token = Request.Params.GetValues("access_token");

    // stage 1 Initial Handshake
    if (codes == null && token == null) {
        return Redirect(twitchBridge.AuthorizeUrl());
    }
    // stage 2 We have a code so we are at stage two request the token
    else if (codes != null) {
        return Redirect(twitchBridge.RetrieveToken(codes[0]));
    }

    // SAVE OAUTH STUFF DOWN HERE.
    // THEN REDIRECT
    return RedirectToAction("Index", "Home");
}

上述操作似乎有效,但是对于第 1 阶段和第 2 阶段,但是当我收到第 2 阶段的回复时,我被重定向到看起来像的 URL。

http://localhost:58434/Home/AuthorizeTwitch#access_token=anaccesstoken&scope=user_read

这符合我的操作,但我无法访问 access_token 的值。我可以访问 Request.Url.OriginalString 但 return 字符串看起来像。

http://localhost:58434/Home/AuthorizeTwitch

调试并查看请求对象和 URL 对象似乎从 # 开始没有存储任何东西。我怀疑这与我网站的路由设置有关。

被击中的相关路线是

routes.MapRoute(
    name: "Default2",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

谁能看出我做错了什么?

如果这与我实际请求抽搐有关,这就是我构建 url 的方式。

public string AuthorizeUrl() {
    return string.Format(
        "{0}{1}?response_type=code&client_id={2}&redirect_uri={3}&scope={4}",
        TwitchUrlBase,
        TwitchAuthorizeMethod,
        ClientId,
        HttpUtility.UrlEncode(TwitchAuthorizeRedirectURL),
        TwitchAuthorizeScope
    );
}

public string RetrieveToken(string code) {
    return string.Format(
        "{0}{1}?response_type=token&client_id={2}&client_secret={3}grant_type=authorization_code&redirect_uri={4}&scope={5}",
        TwitchUrlBase,
        TwitchAuthorizeMethod,
        ClientId,
        ClientSecret,
        HttpUtility.UrlEncode(TwitchAuthorizeRedirectURL),
        TwitchAuthorizeScope
    );
}

fragment 不应该由客户端发送到服务器,因此它不存在是有道理的。您是要发送查询吗?