我是使用WCF创建Web服务的新方法。我只是有一个简短的问题。我正在使用SimpleMembershipProvider验证用户。在我的Web服务的一些调用中,我需要检索当前的用户ID。所以,我正在使用我在MVC.NET中使用的WebSecurity.CurrentUserId
。
为了更好地解释,这是我的web.config,你可以看到,我正在使用自定义验证器:
<system.serviceModel>
...
...
...
<behaviors>
<serviceBehaviors>
<behavior name="behaviorCfg">
...
...
...
<serviceCredentials>
<userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="WarehouseWebService.WarehouseValidator, WarehouseWebService" />
</serviceCredentials>
</behavior>
</serviceBehaviors>
</behaviors>
...
...
...
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" minFreeMemoryPercentageToActivateService="0" multipleSiteBindingsEnabled="true" />
<system.serviceModel>
这是我的自定义验证器(WarehouseValidator.cs):
public class WarehouseValidator : UserNamePasswordValidator
{
public override void Validate(string userName, string password)
{
if (!WebSecurity.Initialized)
WebSecurity.InitializeDatabaseConnection("warehouseConnStr", "UserProfile", "UserId", "UserName", false);
if (!WebSecurity.Login(userName, password, true))
throw new FaultException("Invalid username or password.");
}
}
现在,在webservice上的一些调用(函数)中,我需要获取CurrentUserId并使用WebSecurity.CurrentUserId
,但它返回-1。
所有其他财产似乎也没有被初始化。例如,WebSecurity.CurrentUserName
为空,WebSecurity.IsAuthenticated
为空。
这很奇怪,因为WebSecurity.Login(userName, password, true)
是成功的,我在我的网站(MVC.NET)中使用相同的方法,它运行良好。
我知道这与饼干有关,我甚至试图这样做:
FormsAuthentication.SetAuthCookie(userName, true);
但到目前为止还没有任何效果!
知道为什么吗?
干杯
答案 0 :(得分:0)
如果您使用的是FormsAuthentication.SetAuthCookie(userName, true);
,则需要在配置文件中将aspNetCompatibilityEnabled
设置为true
,这表明您的服务将参与ASP.NET管道;所以可以使用ASP.NET cookie这样的项目。
所以尝试添加
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />