2015-12-04 77 views
1

时,获取完整的url无法正常工作你好堆栈溢出窥探,当url包含#

在实现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,但是当我得到重定向到看起来像一个URL的响应形式阶段2。

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

这个命中我的动作,但我不能访问access_token的值。我可以访问Request.Url.OriginalString,但返回字符串看起来像。

http://localhost:58434/Home/AuthorizeTwitch 

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

正在被击中的相关路线

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

任何一个可以看到我做错了吗?

万一它是与我的实际请求抽搐这是如何建立网址。

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 
    ); 
} 

回答

1

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

+0

那么这个url来自我抽取的令牌的回应。对我来说,这似乎很奇怪,它回到了#哪里?会使约十亿次更有意义,但我认为服务器应该仍然得到整个网址,所以我应该能够看到它的请求。 – Spaceman

+0

是否有一些配置告诉Twitch要去哪个URL?这可能需要一个尾随'?'也许? – James

+0

这是一个好主意,生病检查 – Spaceman