Authentication 有没有办法指定在特定服务类上使用哪个IAuthProvider进行身份验证?
我在同一项目中有两项服务:Authentication 有没有办法指定在特定服务类上使用哪个IAuthProvider进行身份验证?,authentication,jwt,servicestack,basic-authentication,asp.net-core-2.1,Authentication,Jwt,servicestack,Basic Authentication,Asp.net Core 2.1,我在同一项目中有两项服务: [Authenticate] public class OnlyDoesBasicAuth : Service { } [Authenticate] public class OnlyDoesJwtAuth : Service { } //AppHost public override void Configure(Container container) {
[Authenticate]
public class OnlyDoesBasicAuth : Service
{
}
[Authenticate]
public class OnlyDoesJwtAuth : Service
{
}
//AppHost
public override void Configure(Container container)
{
Plugins.Add(new AuthFeature(
() => new AuthUserSession(),
new IAuthProvider[]
{
new BasicAuthProvider(AppSettings),
new JwtAuthProvider(AppSettings)
}
)
{
HtmlRedirect = null
});
}
通过这种设置方式,我可以使用基本身份验证或有效的承载令牌进入两种服务。我更愿意使用它,这样只有一个服务可以执行一种身份验证方式。是否有一种方法可以指定在身份验证时使用的with provider
我不知道如何处理这个问题。我在想,也许有一种方法可以通过全局请求过滤器或其他类似的方式实现,但也许这不是一种方法。我可以将项目分为两个项目,这肯定会奏效,但这不是我想要的方式
我只是在寻找一种方法,使OnlyDoesBasicAuth仅在身份验证时使用BasicAuthProvider,而OnlyDoesJwtAuth仅在同一项目中使用JwtAuthProvider。如有任何建议,我们将不胜感激。
提前感谢。通常,一旦使用任何身份验证提供程序对您进行了身份验证,您就会被视为ServiceStack中无处不在的经过身份验证的用户 您可以通过在
[Authenticate]
属性中指定AuthProvider名称来限制访问,以便需要对服务进行身份验证,例如:
[Authenticate(BasicAuthProvider.Name)]
public class OnlyDoesBasicAuth : Service
{
}
[Authenticate(JwtAuthProvider.Name)]
public class OnlyDoesJwtAuth : Service
{
}
或者,您可以在服务中验证它们是否需要使用特定的身份验证提供商进行身份验证,例如:
if (SessionAs<AuthUserSession>().AuthProvider != JwtAuthProvider.Name)
throw HttpError.Forbidden("JWT Required");
if(SessionAs().AuthProvider!=JwtAuthProvider.Name)
抛出HttpError.Forbidden(“需要JWT”);