【发布时间】:2015-03-17 11:08:18
【问题描述】:
我正在尝试对 WebApi 实施 OAuth 身份验证,我已经使用方法创建了控制器(直接来自示例):
[OverrideAuthentication]
[HostAuthentication(DefaultAuthenticationTypes.ExternalCookie)]
[AllowAnonymous]
[Route("ExternalLogin", Name = "ExternalLogin")]
public IHttpActionResult GetExternalLogin(string provider, string error = null)
{
string redirectUri = string.Empty;
if (error != null)
{
// However google api returns 'access_denied' as error.
return BadRequest(Uri.EscapeDataString(error));
}
if (!User.Identity.IsAuthenticated)
{
// This is runned on first execution.
return new ChallengeResult(provider, this);
}
// Here we should continue with google api callback.
... Rest doesnt matter here.
挑战结果:
public class ChallengeResult : IHttpActionResult
{
public string LoginProvider { get; set; }
public HttpRequestMessage Request { get; set; }
public ChallengeResult(string loginProvider, ApiController controller)
{
LoginProvider = loginProvider;
Request = controller.Request;
}
public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
{
Request.GetOwinContext().Authentication.Challenge(LoginProvider);
var response = new HttpResponseMessage(HttpStatusCode.Unauthorized);
response.RequestMessage = Request;
return Task.FromResult(response);
}
}
GetExternalLogin 方法被调用了两次,第一次来自我,然后 api 将 ChallengeResult 发送给谷歌。我被重定向到谷歌网站并询问有效范围的问题(我可以访问。例如电子邮件、个人资料信息等),我按是,是的,对我来说一切都很好。然而,在那之后,谷歌回调返回 'access_denied' 错误字符串给这个方法。
知道可能出了什么问题吗?我使用的电话是:
http://localhost:8080/api/Account/ExternalLogin?provider=Google&response_type=token&client_id=49235566333-78t8252p46lo75j5e52vda3o1t8fskgc.apps.googleusercontent.com&redirect_uri=http://localhost:8080
Client_id 被替换为虚拟帐户。
redirect_uri 已正确定义到 google 控制台,如果不正确,则错误会有所不同。
试过了: Listing Circles with Google+ for Domains API fails in access_denied 但 id:s 相同。
编辑: 经过数小时的调试发现我的解决方案和示例之间的问题是 Microsoft.Owing.Security.Google 包。在示例中使用的版本是 2.1.0,如果我将其更新为 3.0.0,则会出现此问题。
目前还不知道根本原因。
【问题讨论】:
标签: c# asp.net-web-api oauth