C# ASP.NET核心2.0-[Authorize]doen';t阻止未经授权的用户访问rest api
我正在学习ASP.NET核心。我已经成功地实现了一个C# ASP.NET核心2.0-[Authorize]doen';t阻止未经授权的用户访问rest api,c#,asp.net-core,authorization,openiddict,C#,Asp.net Core,Authorization,Openiddict,我正在学习ASP.NET核心。我已经成功地实现了一个openiddict来保护我的api。成功登录后,用户获得一个令牌,该令牌用于访问web api,但它也允许未经授权的用户(即没有令牌的用户) 控制员就是这样安排的 namespace ISIA.Controllers { [Authorize] [Route("api/[controller]")] public class PostController: Controller { private readonly I
openiddict
来保护我的api。成功登录后,用户获得一个令牌,该令牌用于访问web api,但它也允许未经授权的用户(即没有令牌的用户)
控制员就是这样安排的
namespace ISIA.Controllers
{
[Authorize]
[Route("api/[controller]")]
public class PostController: Controller
{
private readonly IPostService _postService;
private readonly PostToPostViewModelMapper _mapper;
public PostController(
IPostService postService
)
{
_postService = postService;
_mapper = new PostToPostViewModelMapper();
}
[HttpPost]
public ObjectResult SavePost([FromBody] PostViewModel postViewModel)
{
//method body
}
[HttpGet]
public ObjectResult GetAllPost()
{
//method body
}
}
}
身居高位
services.AddOpenIddict(options =>
{
options.AddEntityFrameworkCoreStores<ApplicationDbContext>();
options.AddMvcBinders();
options.EnableAuthorizationEndpoint("/connect/authorize")
.EnableLogoutEndpoint("/connect/logout")
.EnableTokenEndpoint("/connect/token")
.EnableUserinfoEndpoint("/api/userinfo");
options.AllowAuthorizationCodeFlow();
options.RequireClientIdentification();
options.AllowPasswordFlow();
options.AllowRefreshTokenFlow();
options.DisableHttpsRequirement();
options.UseRollingTokens(); //Uncomment to renew refresh tokens on every refreshToken request
// options.AddSigningKey(new SymmetricSecurityKey(System.Text.Encoding.ASCII.GetBytes(Configuration["STSKey"])));
options.Configure(
config =>
{
// Enable sliding expiration
config.UseSlidingExpiration = true;
config.AccessTokenLifetime = TimeSpan.FromMinutes(240);
config.RefreshTokenLifetime = TimeSpan.FromDays(15);
});
});
services.AddOpenIddict(选项=>
{
options.AddEntityFrameworkCoreStores();
options.AddMvcBinders();
options.EnableAuthorizationEndpoint(“/connect/authorize”)
.EnableLogoutEndpoint(“/connect/logout”)
.EnableTokenEndpoint(“/connect/token”)
.EnableUserinfoEndpoint(“/api/userinfo”);
options.AllowAuthorizationCodeFlow();
options.RequireClientIdentification();
options.AllowPasswordFlow();
options.AllowRefreshTokenFlow();
选项。禁用HttpSrequirement();
options.UseRollingTokens();//取消注释以在每次刷新令牌请求时更新刷新令牌
//options.AddSigningKey(新的SymmetricSecurityKey(System.Text.Encoding.ASCII.GetBytes(配置[“STSKey]”));
选项。配置(
配置=>
{
//启用滑动过期
config.UseSlidingExpiration=true;
config.AccessTokenLifetime=TimeSpan.FromMinutes(240);
config.refreshtTokenLifetime=TimeSpan.FromDays(15);
});
});
我做错了什么,请帮我解决。在
授权
属性中设置身份验证方案
,如下所示:
[Authorize(AuthenticationSchemes =
OpenIddictValidationDefaults.AuthenticationScheme)]
这将确保使用OAuth令牌而不是cookie完成授权
OpenIddictValidationDefaults.AuthenticationScheme
用特定的方案授权
如果你的评论表明它失败了,那么你也需要这样做。看起来是这样的:
services
.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.Audience = "https://localhost:5001/";
options.Authority = "http://localhost:5000/";
});
在我的情况下,我愚蠢地试图变得圆滑,只想和别人打交道
services.AddMvcCore()
.AddFormatterMappings()
.AddJsonFormatters()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
在我的ConfigureServices()
中,我错误地尝试优化和精简。好吧,这一定优化了一些核心身份验证管道。尽管我的处理程序总是被调用,但每个请求仍然会发送给控制器
通过添加到核心管道解决:
services.AddMvcCore()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_2)
.AddFormatterMappings()
.AddJsonFormatters()
.AddAuthorization();
您正在调用
app.UseAuthentication()代码>?我在app.UseMvc()之前做过,但顺序可能不重要。是的,我做过。它就在app.UserMvc()的上方。
如果您的web应用程序和api在同一个域(同一个项目)上,那么每个ajax请求都会发送cookies,因此,即使您未在授权
标头中设置令牌,cookie auth也允许该请求。无效操作异常:未配置任何身份验证处理程序对scheme:Bearer进行身份验证
。我怎么解决这个问题。该错误表示您尚未配置令牌身份验证处理程序。您可能需要添加AddJwtBearer
。我已经更新了我的答案。受众
和权限
的详细信息取决于您的具体配置和要求。受众
是令牌的预期消费者,机构
是令牌的预期生产者。