我有这个 WCF 服务,我正在尝试在其中应用身份验证和授权机制。
这是我第一次这样做,我所拥有的是该serviceModel
服务的 web.config 标记:
<system.serviceModel>
<services>
<service name="RoleBasedServices.SecureServiceExternal" behaviorConfiguration="externalServiceBehavior">
<endpoint contract="AuthService.IService1" binding="wsHttpBinding" bindingConfiguration="wsHttpUsername" />
</service>
</services>
<bindings>
<wsHttpBinding>
<binding name="wsHttpUsername">
<security mode="Message">
<message clientCredentialType="UserName" negotiateServiceCredential="false" establishSecurityContext="false" />
</security>
</binding>
</wsHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior>
<!--To avoid disclosing metadata information, set the values below to false before deployment-->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<!--To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information-->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
<behavior name="externalServiceBehavior">
<serviceAuthorization principalPermissionMode="UseAspNetRoles" />
<serviceCredentials>
<userNameAuthentication userNamePasswordValidationMode="MembershipProvider" />
<serviceCertificate findValue="RPKey" x509FindType="FindBySubjectName" storeLocation="LocalMachine" storeName="My"/>
</serviceCredentials>
</behavior>
</serviceBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
我想做的很简单,我不知道我是否需要我正在尝试的所有这些标签。我想要做的是从客户端为服务添加引用并首先调用MyLogin
:
AuthService.Service1Client s = new AuthService.Service1Client();
s.Login();
然后调用另一个受限方法并让它成为GetData
:
s.GetData()
在Login
方法的服务端,仅出于测试目的,我这样做:
public void Login()
{
Thread.CurrentPrincipal = new GenericPrincipal(new GenericIdentity("Bob"), new[] { "Admin" });
FormsAuthentication.SetAuthCookie("BobUserName", false);
}
受限制的方法将是:
[PrincipalPermission(SecurityAction.Demand, Role = "Admin")]
public void GetData()
{
return "Hello";
}
我在服务和客户方面所拥有的一切,我缺少什么?每次,在调试中,我都会检查我发现equalsThread.CurrentPrincipal
的方法,但即使当客户端调用该方法时它也是. PS:我正在使用控制台应用程序进行测试,这有什么不同吗?
谢谢Login
Thread.CurrentPrincipal.Identity.IsAuthenticated
true
GetData()
Access Denied