WCF RIA服务表单身份验证ServiceContext.User.Identity.Name为空

时间:2012-10-01 10:42:31

标签: forms-authentication wcf-ria-services silverlight-5.0 custom-membershipprovider

我们正在使用带有silverlight 5项目的WCF Ria服务。对于身份验证,我们在RIA Services类库中使用自定义成员资格提供者.WCF Ria服务。 客户端身份验证正在运行。我们使用WebContext.Current.User.Name.But服务器端ServiceContext.User访问当前用户名。如果我们使用[RequireAuthentication] attr。在DomainServices中返回始终拒绝访问。 我们如何将WebContext.Current.user推送到ServiceContext.user.I阅读几个文档和教程,但每个测试都失败了。

代码示例:

CustomMembershipProvider.cs:

   public class CustomMembershipProvider : MembershipProvider    {
    public override bool ValidateUser(string username, string password)
    {
        using (var context = new TimEntities())
        {
            var user = context.User.FirstOrDefault(u => u.Username == username &&
                                                         u.Password == password);
            return user != null;
        }
    }

}

AuthenticationDomainService:

      [EnableClientAccess]
      public class AuthenticationDomainService : AuthenticationBase<AuthUser>
      {}
      public class AuthUser : UserBase
      {}

App.Xaml.Cs:

  var webContext = new WebContext();
  var formsAuth = new FormsAuthentication();
  var authContext = new AuthenticationDomainContext();
  formsAuth.DomainContext = authContext;
  webContext.Authentication = formsAuth;
  ApplicationLifetimeObjects.Add(webContext);

2 个答案:

答案 0 :(得分:2)

我遇到了同样的问题,但我终于找到了答案。我一直在试图暴露SOAP端点,因此我可以从手机应用程序访问相同的RIA服务。作为其中的一部分,我在App构造函数中添加了以下行:

WebRequest.RegisterPrefix("http://", WebRequestCreator.ClientHttp);

摆脱它,你应该再次使用用户名。 SOAP端点似乎仍然暴露在手机应用程序中。

答案 1 :(得分:0)

首先,无论您是否使用WCF RIA服务,您的托管网站都必须configure Forms authentication。此外,必须安装表单身份验证并enabled on IIS

然后您必须配置ASP.NET membership才能使用CustomMembershipProvider课程。