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

Forms authentication WCF RIA服务表单身份验证ServiceContext.User.Identity.Name为空,forms-authentication,wcf-ria-services,silverlight-5.0,custom-membershipprovider,Forms Authentication,Wcf Ria Services,Silverlight 5.0,Custom Membershipprovider,我们正在silverlight 5项目中使用WCF Ria服务。对于身份验证,我们使用Ria服务类库中的Custom membership provider.WCF Ria服务。 客户端身份验证正在运行。我们使用WebContext.current.user.name访问当前用户名。但服务器端ServiceContext.user为空。如果我们使用[RequireAuthentication]属性。域内服务返回始终拒绝访问。 如何将WebContext.Current.user推送到Servic

我们正在silverlight 5项目中使用WCF Ria服务。对于身份验证,我们使用Ria服务类库中的Custom membership provider.WCF Ria服务。 客户端身份验证正在运行。我们使用WebContext.current.user.name访问当前用户名。但服务器端ServiceContext.user为空。如果我们使用[RequireAuthentication]属性。域内服务返回始终拒绝访问。 如何将WebContext.Current.user推送到ServiceContext.user。我读了好几篇文档和教程,但每次测试都失败了

  var webContext = new WebContext();
  var formsAuth = new FormsAuthentication();
  var authContext = new AuthenticationDomainContext();
  formsAuth.DomainContext = authContext;
  webContext.Authentication = formsAuth;
  ApplicationLifetimeObjects.Add(webContext);
代码示例:

  var webContext = new WebContext();
  var formsAuth = new FormsAuthentication();
  var authContext = new AuthenticationDomainContext();
  formsAuth.DomainContext = authContext;
  webContext.Authentication = formsAuth;
  ApplicationLifetimeObjects.Add(webContext);
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;
        }
    }
  var webContext = new WebContext();
  var formsAuth = new FormsAuthentication();
  var authContext = new AuthenticationDomainContext();
  formsAuth.DomainContext = authContext;
  webContext.Authentication = formsAuth;
  ApplicationLifetimeObjects.Add(webContext);
}

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

      [EnableClientAccess]
      public class AuthenticationDomainService : AuthenticationBase<AuthUser>
      {}
      public class AuthUser : UserBase
      {}
  var webContext = new WebContext();
  var formsAuth = new FormsAuthentication();
  var authContext = new AuthenticationDomainContext();
  formsAuth.DomainContext = authContext;
  webContext.Authentication = formsAuth;
  ApplicationLifetimeObjects.Add(webContext);

我也有同样的问题,但我终于找到了答案。我一直在尝试公开SOAP端点,这样我就可以从手机应用程序访问相同的RIA服务。作为其中的一部分,我在应用程序构造函数中添加了以下行:

  var webContext = new WebContext();
  var formsAuth = new FormsAuthentication();
  var authContext = new AuthenticationDomainContext();
  formsAuth.DomainContext = authContext;
  webContext.Authentication = formsAuth;
  ApplicationLifetimeObjects.Add(webContext);
WebRequest.RegisterPrefix("http://", WebRequestCreator.ClientHttp);
去掉它,您应该可以再次使用用户名。SOAP端点似乎也会暴露在手机应用程序中。

首先,无论您是否使用WCF RIA服务,您都必须为您的托管网站提供服务。此外,还必须安装和安装表单验证

  var webContext = new WebContext();
  var formsAuth = new FormsAuthentication();
  var authContext = new AuthenticationDomainContext();
  formsAuth.DomainContext = authContext;
  webContext.Authentication = formsAuth;
  ApplicationLifetimeObjects.Add(webContext);
然后您必须进行配置才能使用
CustomMembershipProvider

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