c# - 来自 web api : access_denied error 的谷歌身份验证

标签 c# asp.net-web-api oauth

我正在尝试对 WebApi 实现 OAuth 身份验证,我已经使用以下方法创建了 Controller (直接来自示例):

    [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 发送给 google。我被重定向到谷歌网站并询问有效范围的问题(我可以访问。例如电子邮件、个人资料信息等),我按是是的,对我来说一切都很好。然而,在该谷歌回调返回“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,则会出现此问题。

尚不清楚根本原因。

最佳答案

我也有这个问题。要解决此问题,请尝试修改您的 Google 应用程序以使用 Google + API。我以前只使用“Identity Toolkit API”。根据 Pranav 指出的文章,当你升级到 Google Middleware 3.0.0(Microsoft.Owin.Security.Google) 时,你需要使用 Google + API。

关于c# - 来自 web api : access_denied error 的谷歌身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25469347/

相关文章:

c# - 无法将 System.Data.Spatial 添加到我的域类

c# - Entity Framework 6 + Oracle如何查询两个结构相同的表

c# - 如何在 C# 中获取 ConcurrentDictionary 的即时快照?

c# - Entity Framework : more than one entity have the same primary key value

javascript - 在 http 请求之前检查访问 token

c# - 使用增量数字作为 GetHashCode 返回值

mysql - 无法使用 nuget 在 Visual Studio 中安装 MySql.Data

c# - 在 ODataController 中添加 WebAPI OData $orderby 选项

java - Spring Boot 和 OAuth2 : Getting ResourceAccessException: I/O error on POST request for "http://localhost:5555/oauth/token": Connection refused: connect

java - 在 Android 应用程序中将 Web 应用程序的 oauth 请求结果 Hook 到 Twitter