web api 2 - Oauth2 不记名令牌访问被禁止
web api 2 - Oauth2 Bearer token access is forbidden
我有一个带有 Web 的 mvc 5 应用程序 api 2. (.NET 4.6)
我在我的 mvc 应用程序的身份验证旁边实施了 oauth2 配置 (app.UseCookieAuthentication) :
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
我也用postman测试过,结果一样。
有没有人遇到过同样的问题?
你知道我错过了什么吗?
更新:
如果我将我的应用程序部署在服务器上(Azure 应用程序服务)但仍然不在我的机器上,它就可以工作
我找到问题的原因了!
我正在为我的开发人员使用特技演员 (https://rimdev.io/stuntman/),但我忘记为 oauth 配置它 ...
缺少这一行:
StuntmanOptions.AllowBearerTokenPassthrough = true;
我有一个带有 Web 的 mvc 5 应用程序 api 2. (.NET 4.6) 我在我的 mvc 应用程序的身份验证旁边实施了 oauth2 配置 (app.UseCookieAuthentication) :
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
我也用postman测试过,结果一样。
有没有人遇到过同样的问题?
你知道我错过了什么吗?
更新: 如果我将我的应用程序部署在服务器上(Azure 应用程序服务)但仍然不在我的机器上,它就可以工作
我找到问题的原因了!
我正在为我的开发人员使用特技演员 (https://rimdev.io/stuntman/),但我忘记为 oauth 配置它 ...
缺少这一行:
StuntmanOptions.AllowBearerTokenPassthrough = true;