Tôi đã làm theo hướng dẫn để bảo vệ API Web với OAuth trong C#.Điểm cuối API trả lại "Ủy quyền đã bị từ chối cho yêu cầu này". khi gửi mã thông báo mang số
Tôi đang thực hiện một số thử nghiệm và cho đến nay tôi đã có thể nhận được mã thông báo truy cập thành công từ /token
. Tôi đang sử dụng một tiện ích mở rộng của Chrome có tên là "Khách hàng REST nâng cao" để kiểm tra nó.
{"access_token":"...","token_type":"bearer","expires_in":86399}
Đây là những gì tôi nhận được từ /token
. Mọi thứ trông có vẻ tốt.
yêu cầu tiếp theo của tôi là để điều khiển API thử nghiệm của tôi:
namespace API.Controllers
{
[Authorize]
[RoutePrefix("api/Social")]
public class SocialController : ApiController
{
....
[HttpPost]
public IHttpActionResult Schedule(SocialPost post)
{
var test = HttpContext.Current.GetOwinContext().Authentication.User;
....
return Ok();
}
}
}
Yêu cầu là một POST
và có tiêu đề:
Authorization: Bearer XXXXXXXTOKEHEREXXXXXXX
tôi nhận được: Authorization has been denied for this request.
trả lại trong JSON.
Tôi đã thử thực hiện GET và tôi nhận được những gì tôi mong đợi, rằng phương pháp này không được hỗ trợ vì tôi không triển khai nó.
Đây là nhà cung cấp ủy quyền của tôi:
public class SimpleAuthorizationServerProvider : OAuthAuthorizationServerProvider
{
public override async Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context)
{
context.Validated();
}
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });
using (var repo = new AuthRepository())
{
IdentityUser user = await repo.FindUser(context.UserName, context.Password);
if (user == null)
{
context.SetError("invalid_grant", "The user name or password is incorrect.");
return;
}
}
var identity = new ClaimsIdentity(context.Options.AuthenticationType);
identity.AddClaim(new Claim(ClaimTypes.Name, context.UserName));
identity.AddClaim(new Claim(ClaimTypes.Role, "User"));
context.Validated(identity);
}
}
Bất kỳ trợ giúp sẽ là tuyệt vời. Tôi không chắc đó có phải là yêu cầu hay mã sai.
chỉnh sửa: Đây là tôi Startup.cs
public class Startup
{
public void Configuration(IAppBuilder app)
{
var config = new HttpConfiguration();
WebApiConfig.Register(config);
app.UseWebApi(config);
ConfigureOAuth(app);
}
public void ConfigureOAuth(IAppBuilder app)
{
var oAuthServerOptions = new OAuthAuthorizationServerOptions()
{
AllowInsecureHttp = true,
TokenEndpointPath = new PathString("/token"),
AccessTokenExpireTimeSpan = TimeSpan.FromDays(1),
Provider = new SimpleAuthorizationServerProvider()
};
// Token Generation
app.UseOAuthAuthorizationServer(oAuthServerOptions);
app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());
}
}
Làm thế nào bạn sẽ làm điều này nếu bạn không đăng ký WebApi trong đường ống? Tôi chỉ đang làm việc với mẫu API Web mặc định. Yêu cầu GET hoạt động tốt, yêu cầu POST cung cấp cho tôi 401. Trong khi mã thông báo giống nhau. – mwijnands
Điều này khiến tôi hiểu! Cảm ơn bạn đã dành thời gian đăng câu trả lời này. – heymega
Tôi lãng phí vài giờ sau khi tái cấu trúc cố gắng tìm một lý do cho vấn đề này. – Tomino