授权部分Web方法

时间:2014-12-16 15:26:17

标签: c# asp.net web-services authorization

我正在使用ASMX Web服务(不是WCF),我为基本身份验证实现了简单的HTTP模块。 一切正常用于授权文件(web.config deny / allow),User.IsInRole也被填充。

但是,我想授权部分网络方法。为此,我发现只有PrincipalPermission.Demand属性。但是,尽管我传递了有效的登录凭据,但总是抛出异常。

网络配置:

    <system.webServer>
    <modules>
        <add name="AuthenticationModule" type="AuthenticationModule"/>
    </modules>
</system.webServer>
<system.web>
        <authentication mode="None"/>
    <authorization>
        <allow users="*" />
    </authorization>
</system.web>

客户端:

CommonService.CommonServices service = new CommonService.CommonServices();
               service.Credentials = new NetworkCredential(username, pass);
                service.PreAuthenticate = true;
                service.HelloWorldForEveryone();

它抛出:

Unhandled exception: System.Web.Services.Protocols.SoapException: System.Web.Serv
ices.Protocols.SoapException: 
   v System.Security.Permissions.PrincipalPermission.ThrowSecurityException()
   v System.Security.Permissions.PrincipalPermission.Demand()
   v System.Security.PermissionSet.DemandNonCAS()
   v WSAutentization.CommonServices.HelloWorldForEveryone() in CommonServices.asmx.cs:řádek 30
   v System.Web.Services.Protocols.SoapHttpClientProtocol.ReadResponse(SoapClien
tMessage message, WebResponse response, Stream responseStream, Boolean asyncCall
)
   v System.Web.Services.Protocols.SoapHttpClientProtocol.Invoke(String methodNa
me, Object[] parameters)
   v Console.CommonService.CommonServices.HelloWorldForEveryone() v d:\TMP\WSAut
entization\Console\Web References\CommonService\Reference.cs:řádek 93
   v Console.Program.Main(String[] args) v d:\TMP\WSAutentization\Console\Progra
m.cs:line 35

还有另一种方法来授权部分网络方法吗?

非常感谢!

1 个答案:

答案 0 :(得分:0)

针对PrincipalPermission检查

Thread.CurrentPrincipal。 我没有在您的身份验证模块中看到代码,但我遇到了同样的问题,解决方法是设置

Thread.CurrentPrincipal = HttpContext.Current.User;
相关问题