Phải thừa nhận rằng đây là lần đầu tiên thiết lập dự án api lõi Asp.Net. Một yêu cầu là hỗ trợ OAuth2. Máy chủ Api và Identity là hai dự án riêng biệt, cả hai đều bắt đầu từ một mẫu lõi rỗng của Asp.Net.Ủy quyền với Asp.Net Core WebAPI
Máy chủ nhận dạng được thiết lập và đang chạy và mã thông báo đang được cung cấp qua luồng chủ sở hữu tài nguyên. Nhận được mã thông báo là tốt, phạm vi và các chi tiết access_token có liên quan xuất hiện là chính xác.
Khi tôi đưa ra một yêu cầu get để tài nguyên cuối cùng quan điểm của tôi, tôi nhận được sau lúc đầu ...
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
Request starting HTTP/1.1 GET http://localhost:12886/v1/mystuff
info: Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerMiddleware[2]
Successfully validated the token.
info: Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerMiddleware[3]
HttpContext.User merged via AutomaticAuthentication from authenticationScheme: Bearer.
info: Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerMiddleware[8]
AuthenticationScheme: Bearer was successfully authenticated.
info: IdentityModel.AspNetCore.ScopeValidation.ScopeValidationMiddleware[0]
Scopes found on current principal: scope: stuffdetails, scope: stuffmover
info: Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerMiddleware[8]
AuthenticationScheme: Bearer was successfully authenticated.
info: Microsoft.AspNetCore.Authorization.DefaultAuthorizationService[1]
Authorization was successful for user: 939d72dd-654c-447f-a65d-d0426b1eca59.
Vì vậy, tôi có thể nói middleware được xác nhận thẻ của tôi, đọc phạm vi, và chứng thực mã thông báo. Tuy nhiên, ngay sau thành công ban đầu, tôi nhận được các ủy quyền thất bại.
info: Microsoft.AspNetCore.Authorization.DefaultAuthorizationService[2]
Authorization failed for user: 939d72dd-654c-447f-a65d-d0426b1eca59.
info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[1]
Authorization failed for the request at filter 'Microsoft.AspNetCore.Mvc.Authorization.AuthorizeFilter'.
info: Microsoft.AspNetCore.Mvc.ChallengeResult[1]
Executing ChallengeResult with authentication schemes().
info: Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerMiddleware[13]
AuthenticationScheme: Bearer was forbidden.
info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[2]
Executed action TestApi.StuffController.GetStuff (TestApi) in 32.4439ms
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
Request finished in 1207.1769ms 403
Đây là những gì tôi tin là các bit liên quan khi khởi động.
ConfigureServices ...
services.AddMvcCore()
.AddAuthorization(opts =>
{
opts.AddPolicy("stuffdetails",
policy => policy.RequireClaim("stuffdetails"));
}
)
.AddJsonFormatters();
services.AddOptions();
Configure - Lưu ý rằng tôi biết configOptions của tôi là chính xác bởi vì thử thách dấu hiệu ban đầu là thành công.
var authServerOptions = new IdentityServerAuthenticationOptions
{
Authority = configOptions.Value.AuthServerSettings.AuthServerURI,
RequireHttpsMetadata = configOptions.Value.AuthServerSettings.RequiresHttpsMetaData,
ApiName = configOptions.Value.AuthServerSettings.ApiName,
AllowedScopes = configOptions.Value.AuthServerSettings.AllowedScopes,
SupportedTokens = IdentityServer4.AccessTokenValidation.SupportedTokens.Jwt,
AuthenticationScheme = "Bearer",
SaveToken = true,
ValidateScope = true
};
app.UseIdentityServerAuthentication(authServerOptions);
app.UseMvc();
Stuff khiển
[Route("v1/[controller]")]
[Authorize(ActiveAuthenticationSchemes = "Bearer")]
public class StuffController : Controller
{
[HttpGet]
[Authorize(Policy = "stuffdetails")]
public JsonResult GetStuff()
{
return new JsonResult(new
{
Message = "You've got stuff.."
});
}
}
Nếu tôi loại bỏ các thuộc tính Authorize từ phương pháp GetStuff, mọi thứ đều tốt vì như nhật ký cho thấy, các dấu hiệu mang được ủy quyền.
Những câu hỏi:
- Được uỷ quyền thất bại vì chính sách của tôi là không chính xác, và nếu như vậy làm thế nào nó nên được thiết lập?
- Nếu tôi muốn xác thực mã thông báo chứa các xác nhận quyền sở hữu thích hợp và được ủy quyền, có đúng không khi sử dụng các chính sách như tôi có?
- Tôi có mắc lỗi khi cố sử dụng UseIdentityServerAuthentication thay vì UseJwtBearerAuthentication?
Any help is appreciated rất nhiều ..
Bạn đang đặt ra ba câu hỏi hay, mặc dù có ba câu hỏi trong một câu hỏi. Cân nhắc chia câu hỏi của bạn thành ba câu hỏi riêng biệt. Ngay bây giờ, nó hơi quá rộng. –