web api 2 - 禁止Oauth2 Bearer令牌访问

时间:2018-05-19 22:14:04

标签: oauth-2.0 asp.net-web-api2

我有一个带有web api 2的mvc 5应用程序。(.NET 4.6) 我在我的mvc app(app.UseCookieAuthentication)的身份验证旁边实现了oauth2配置:

    OAuthAuthorizationServerOptions OAuthServerOptions = new 
    OAuthAuthorizationServerOptions()
    {
        AllowInsecureHttp = true,
        TokenEndpointPath = new PathString("/oauth/token"),
        Provider = new AspNetIdentityOAuthAuthorizationServerProvider(),
        AccessTokenExpireTimeSpan = TimeSpan.FromMinutes(1000), 
    };

    app.UseOAuthBearerTokens(OAuthServerOptions);

我的api受授权属性(全局过滤器)保护。

我使用客户端凭据授权

我跟着这两篇文章(都是一样的) https://docs.microsoft.com/en-us/aspnet/web-api/overview/security/individual-accounts-in-web-api https://mitchelsellers.com/blogs/2017/05/10/adding-webapi-oauth-authentication-to-an-existing-project

我能够为我的用户获取令牌,但是当我想使用令牌访问我的Api时,我收到了403禁用错误

        HttpClient client = new HttpClient();
        Dictionary<string, string> parameters = new Dictionary<string, string>();
        parameters.Add("grant_type", "client_credentials");
        parameters.Add("client_id", "4rclFahG7gho8erzbsmTbw==");
        parameters.Add("client_secret", "IBSqiYb0kT/lzV0gpQsPxkUDI9ztu0dhHWDe4VQDzKGYm2pl+75sMVfEsoGo4FAxFm0qZUFcDrVMrfqYhn2bzw==");

        var content = new FormUrlEncodedContent(parameters);
        try
        {
            HttpResponseMessage result = client.PostAsync("http://localhost:49594/oauth/token", content).Result;
            string jsonResult = result.Content.ReadAsStringAsync().Result;

            var resultObject = JsonConvert.DeserializeObject<TokenResult>(jsonResult);
            var accessToken = resultObject.access_token;


            client.DefaultRequestHeaders.Add("Authorization", "Bearer " + accessToken);
            result = client.GetAsync("http://localhost:49594/api/v1/echo?id=myt

estvalue").Result;

// RESULT  is 403 - Forbidden

我也和邮递员一起测试过,结果是一样的。

是否有人遇到过同样的问题?

你怎么知道我&#39;失踪?

更新: 如果我在服务器上部署我的应用程序(天蓝色应用程序服务)但仍然不在我的机器上,它可以正常工作

1 个答案:

答案 0 :(得分:0)

我找到了问题的原因!

我正在为我的开发人员使用替身演员(https://rimdev.io/stuntman/),我忘了为oauth配置...

这条线丢失了:

  

StuntmanOptions.AllowBearerTokenPassthrough = true;

相关问题