我正在寻找合适解决方案的日子。我的数据库中存储了用户名和密码。我只希望特定用户能够访问某些控制器方法。如何通过用户ID检查用户ID是否具有权限来实现它?
谢谢!
您可以在Web api中实现基本身份验证,在需要提供者用户凭证的特定控制器上添加[Authorize]属性。请参考下面的代码示例文章:
[Authorize]
https://codeburst.io/adding-basic-authentication-to-an-asp-net-core-web-api-project-5439c4cf78ee
https://beetechnical.com/rest-api/how-to-validate-rest-api-using-basic-authentication-in-web-api-net-core/
https://jasonwatmore.com/post/2019/10/21/aspnet-core-3-basic-authentication-tutorial-with-example-api