如何使用 [Authorize(Roles)] 属性授权操作?
How to authorize actions with the [Authorize(Roles)] attribute?
我正在尝试使用基于单一模型 class 的控制器创建一个简单的 Web API。
我在 Startup.cs
中添加了 AddIdentity
,创建了两个用户角色并尝试在控制器中授权一个操作,以便两个角色之一可以专门访问它。但是,无论我尝试使用什么来测试它,它总是会导致错误,其中响应表明服务器甚至没有输入指定的操作。到目前为止,我尝试使用 Postman 和 Swagger UI,它们都没有给我提供正确的响应。
我要访问的操作是这个:
// GET api/media
[HttpGet]
[Authorize(Roles = "Member")]
public IEnumerable<Media> Get()
{
return _mediaData.Get();
}
以下是我在 Startup.cs
class 中添加身份支持的方式:
services.AddIdentity<User, Role>(config => { config.SignIn.RequireConfirmedEmail = false; })
.AddEntityFrameworkStores<ApplicationDbContext>()
//.AddDefaultUI()
.AddDefaultTokenProviders();
我期望的是一个 JSON 对象,但结果 Swagger 向我提供了一个错误,指出我应该登录,但我不确定我该怎么做。我想我需要学习更多关于 SignIn 和 UserManager 的东西,但我不知道我会如何 "login and open a session" (这就是我认为可以解决问题的方法)而且我一直无法找到任何资源来解释如何做到这一点,更不用说如何通过 Postman 或 Swagger UI.
获得结果了
Swagger UI 为您提供了一种身份验证方法,为此您需要对 Startup.cs
进行一些更改。为此,将您的 ConfigureServices
方法修改为:
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Info { Title = "My API", Version = "v1" });
c.AddSecurityDefinition("basic", new BasicAuthScheme() { Type = "basic" }); // Depending on your authentication, this will look different, but the most common one would be the Basic Authentication, so perhaps play with that or go with what you already had in mind
c.DocumentFilter<BasicAuthDocumentFilter>();
});
如果您使用基本身份验证,则可以使用下面的代码为 Swagger 提供文档过滤器 (BasicAuthDocumentFilter
),它可能如下所示:
public class BasicAuthDocumentFilter : IDocumentFilter
{
public void Apply(SwaggerDocument swaggerDoc, DocumentFilterContext context)
{
var securityRequirements = new Dictionary<string, IEnumerable<string>>()
{
{ "basic", new string[] { } }
};
swaggerDoc.Security = new[] { securityRequirements };
}
}
要使用它,只需打开 Swagger UI,您应该会在标题下方看到一个授权按钮。单击它并提供所需的必要身份验证信息,您就可以开始了。
至于ASP Identity,如果我没有记错的话,使用ASP Identity for Web APIs 并不是很常见。我个人会切换到 Basic 或 OAITH,因为在谈论 API 时它们听起来更自然。
我正在尝试使用基于单一模型 class 的控制器创建一个简单的 Web API。
我在 Startup.cs
中添加了 AddIdentity
,创建了两个用户角色并尝试在控制器中授权一个操作,以便两个角色之一可以专门访问它。但是,无论我尝试使用什么来测试它,它总是会导致错误,其中响应表明服务器甚至没有输入指定的操作。到目前为止,我尝试使用 Postman 和 Swagger UI,它们都没有给我提供正确的响应。
我要访问的操作是这个:
// GET api/media
[HttpGet]
[Authorize(Roles = "Member")]
public IEnumerable<Media> Get()
{
return _mediaData.Get();
}
以下是我在 Startup.cs
class 中添加身份支持的方式:
services.AddIdentity<User, Role>(config => { config.SignIn.RequireConfirmedEmail = false; })
.AddEntityFrameworkStores<ApplicationDbContext>()
//.AddDefaultUI()
.AddDefaultTokenProviders();
我期望的是一个 JSON 对象,但结果 Swagger 向我提供了一个错误,指出我应该登录,但我不确定我该怎么做。我想我需要学习更多关于 SignIn 和 UserManager 的东西,但我不知道我会如何 "login and open a session" (这就是我认为可以解决问题的方法)而且我一直无法找到任何资源来解释如何做到这一点,更不用说如何通过 Postman 或 Swagger UI.
获得结果了Swagger UI 为您提供了一种身份验证方法,为此您需要对 Startup.cs
进行一些更改。为此,将您的 ConfigureServices
方法修改为:
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Info { Title = "My API", Version = "v1" });
c.AddSecurityDefinition("basic", new BasicAuthScheme() { Type = "basic" }); // Depending on your authentication, this will look different, but the most common one would be the Basic Authentication, so perhaps play with that or go with what you already had in mind
c.DocumentFilter<BasicAuthDocumentFilter>();
});
如果您使用基本身份验证,则可以使用下面的代码为 Swagger 提供文档过滤器 (BasicAuthDocumentFilter
),它可能如下所示:
public class BasicAuthDocumentFilter : IDocumentFilter
{
public void Apply(SwaggerDocument swaggerDoc, DocumentFilterContext context)
{
var securityRequirements = new Dictionary<string, IEnumerable<string>>()
{
{ "basic", new string[] { } }
};
swaggerDoc.Security = new[] { securityRequirements };
}
}
要使用它,只需打开 Swagger UI,您应该会在标题下方看到一个授权按钮。单击它并提供所需的必要身份验证信息,您就可以开始了。
至于ASP Identity,如果我没有记错的话,使用ASP Identity for Web APIs 并不是很常见。我个人会切换到 Basic 或 OAITH,因为在谈论 API 时它们听起来更自然。