拦截WCF方法调用并“重新路由”基于身份验证状态
很好奇是否有一种方法可以在进行方法调用之前检查用户是否经过身份验证,然后返回自定义对象,如果没有,则不完成调用。例如:
我们将 global.asax 中的 Application_PostAuthenticateRequest 中的用户主体设置为发出请求的用户(如果经过身份验证)或匿名用户(如果未经过身份验证)。目前,在所有需要身份验证的方法中,我们都有类似于以下代码的内容:
public Result GetSomeObject()
<代码>{
if (HttpContext.Current.Request.IsAuthenticated)
<代码>{
<代码>}}
底线:我们希望能够在让 WCF 进入需要身份验证的方法之前检查 IsAuthenticated,如果为真,则继续,如果不是,则返回一个 Result 对象 (JSON)身份验证错误。
Was curious if there was a way to check if a user is authenticated before making a method call and then returning a custom object and not completing the call if they aren't. For example:
We set the user principal in the Application_PostAuthenticateRequest in the global.asax to the user making the request if they are authenticated or to an anonymous user if they aren't. Currently in all methods that require authentication we have something similar to the following code:
public Result GetSomeObject()
{
if (HttpContext.Current.Request.IsAuthenticated)
{
}
}
Bottom line: We want to be able to check IsAuthenticated BEFORE we let WCF get inside the method on methods that require authentication, and if true, continue, if not, return a Result object (JSON) with an authentication error.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你需要开发的是WCF中的ServiceAuthorizationManager。您可以在以下位置找到更多信息:
What you need to develop is called ServiceAuthorizationManager in WCF. You can find more information about this on:
http://pieterderycke.wordpress.com/2011/04/07/implementing-restricted-access-to-a-wcf-service-with-the-serviceauthorizationmanager/
您可以编写自定义的 httpmodule 来拦截对服务层的请求并在其中进行身份验证。
You can write a custom httpmodule to intercept the requests to the service layer and do the authentication in there.