如何使用 ServiceStack 身份验证阻止访问特定路径? [英] How can I prevent access to specific path with ServiceStack Authentication?
本文介绍了如何使用 ServiceStack 身份验证阻止访问特定路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我们想使用默认的 asp.net 身份验证阻止访问特定路径时,我们这样做:
When we want to prevent access to specific path with default asp.net authentication, we do:
<location path="routes.axd">
<system.web>
<authorization>
<allow roles="Agent"/>
<deny users="*"/>
</authorization>
</system.web>
我们如何使用 ServiceStack?
How can we do with ServiceStack?
推荐答案
ServiceStack 中没有保护/paths 的配置.
There is no configuration to protect /paths in ServiceStack.
您可以通过在任一操作上添加 [Authenticate]
属性来保护服务:
You can protect services by adding the [Authenticate]
attribute on either the Action:
class MyService : Service {
[Authenticate]
public object Get(Protected request) { ... }
}
请求 DTO
[Authenticate]
class Protected { ... }
或者服务实现
[Authenticate]
class MyService : Service {
public object Get(Protected request) { ... }
}
或者从基类继承
[Authenticate]
class MyServiceBase : Service { ... }
class MyService : MyServiceBase {
public object Get(Protected request) { ... }
}
使用全局请求过滤器
否则,如果您可以使用全局请求过滤器,如果您想以任何其他方式限制所有请求,例如:
Using a Global Request Filter
Otherwise if you can use a global Request Filter if you wanted to restrict all requests any other way, e.g something like:
appHost.RequestFilters.Add((httpReq, httpResp, requestDto) =>
{
if (IsAProtectedPath(httpReq.PathInfo)) {
new AuthenticateAttribute()
.Execute(httpReq, httpResp, requestDto);
}
});
这篇关于如何使用 ServiceStack 身份验证阻止访问特定路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文