【发布时间】:2021-06-24 21:19:51
【问题描述】:
对不起,如果这是非常基本的,但我以前从未做过这样的事情。
在我的应用中,我们使用 Azure AD B2C 来照顾我们的用户。使用端点时,我们需要一个包含范围的 JWT 令牌,现在说“TestScope”。目前我有这个有效的代码:
[Authorize]
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
// The web API will only accept tokens 1) for users, and 2) having the "TestScope" scope for this API
static readonly string[] scopeRequiredByApi = new string[] { "TestScope" };
[HttpGet]
public string Get()
{
HttpContext.VerifyUserHasAnyAcceptedScope(scopeRequiredByApi);
return "test";
}
}
我的想法是,不是每次创建端点时都使用这两行代码,而是可以创建一个自定义属性,例如 '[Authorise(Scope = "TestScope")]' 来做同样的事情吗?或者可能是一个“[TestScope]”标签,我不必编写字符串并且可能会犯拼写错误,尽管我想如果我这样做会很快很明显。
感谢您的建议!欣赏它
【问题讨论】:
-
我认为,如果您使用的是 Microsoft.Identity.Web,您应该能够使用类似:[AuthorizeForScopes(Scopes = new[] { "TestScope" })]
标签: c# asp.net annotations