Sky*_*rsZ 5 c# .net-core asp.net-core .net-core-3.0
我目前正在使用 VSCode 处理 .net 核心 api 项目,并且我想将 ASP.NET 核心身份验证和授权与 cookie 集成。
我试图添加services.AddAuthentication();
和services.AddAuthorization();
在Startup.cs
。我还在[Authorize]
我的UsersController.cs
.
[Authorize]
[Route("api/Users")]
[ApiController]
public class UsersController : ControllerBase
Run Code Online (Sandbox Code Playgroud)
问题是当我启动我的应用程序时,即使我没有创建用户或角色,我仍然可以访问我的 API。( https://localhost:5001/api/Users
)
我尝试设置launchSettings.json
如下:
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": false,
"iisExpress": {
"applicationUrl": "http://localhost:49086",
"sslPort": 44343
}
Run Code Online (Sandbox Code Playgroud)
但我仍然可以访问我的 API。如果没有用户或角色,如何拒绝访问?
我肯定错过了一些东西,但我不知道是什么。
预先感谢您的帮助!
好吧,我有点傻,我从 ASP Mvc 5 的相关问题中找到了这个解决方案,我Startup.cs
在我的以下行中添加了以下内容ConfigurationServices
:
services.AddMvcCore(option => option.EnableEndpointRouting = false).AddAuthorization();
Run Code Online (Sandbox Code Playgroud)
并在Configure
:
app.UseMvc();
Run Code Online (Sandbox Code Playgroud)
现在,[Authorize]
在我的控制器中,如果我没有经过身份验证,我将无法访问我的 API。
归档时间: |
|
查看次数: |
97 次 |
最近记录: |