【发布时间】:2016-03-08 20:37:35
【问题描述】:
你好堆栈溢出窥视,
在实现 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 但返回字符串看起来像。
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
);
}
【问题讨论】:
标签: c# asp.net asp.net-mvc url twitch