自定义身份验证域服务 - Silverlight和RIA

时间:2011-12-17 20:43:25

标签: authentication silverlight-4.0 forms-authentication ria

我正在尝试编写自定义身份验证域服务。我想我理解了this blog上写的所有代码。

但是我不知道如何指定应该使用哪个域服务应用程序。我有一个抽象的域服务,第二个是这个服务的具体实现。如果我构建整个解决方案,我会收到错误

'MainModule.Web.FormsAuthenticationService`1' is not a valid DomainService type. DomainService types cannot be abstract or generic.

我在博客上找不到我之前提到过的源代码。

namespace MainModule.Web
{
    using System;
    using System.ServiceModel.DomainServices.Hosting;
    using System.ServiceModel.DomainServices.Server;



    // TODO: Create methods containing your application logic.
    [EnableClientAccess()]
    public abstract class FormsAuthenticationService<TUser> : DomainService, IAuthentication<TUser> where TUser : UserBase
    {

        protected abstract TUser GetCurrentUser(string name, string userData);
        protected abstract TUser ValidateCredentials(string name, string password, string customData, out string userData);
        protected virtual TUser GetDefaultUser()
        {
            return null;
        }

        public TUser GetUser()
        {
            IPrincipal currentUser = ServiceContext.User;
            if ((currentUser != null) && currentUser.Identity.IsAuthenticated)
            {
                FormsIdentity userIdentity = currentUser.Identity as FormsIdentity;
                if (userIdentity != null)
                {
                    FormsAuthenticationTicket ticket = userIdentity.Ticket;
                    if (ticket != null)
                    {
                        return GetCurrentUser(currentUser.Identity.Name, ticket.UserData);
                    }
                }
            }

            return GetDefaultUser();
        }

        public TUser Login(string userName, string password, bool isPersistent, string customData)
        {
            string userData;
            TUser user = ValidateCredentials(userName, password, customData, out userData);

            if (user != null)
            {
                FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(/* version */ 1, userName,
                                                           DateTime.Now, DateTime.Now.AddMinutes(30),
                                                           isPersistent,
                                                           userData,
                                                           FormsAuthentication.FormsCookiePath);

                string encryptedTicket = FormsAuthentication.Encrypt(ticket);
                HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);

                HttpContextBase httpContext = (HttpContextBase)ServiceContext.GetService(typeof(HttpContextBase));
                httpContext.Response.Cookies.Add(authCookie);
            }
            else
            {
                HttpContextBase httpContext = (HttpContextBase)ServiceContext.GetService(typeof(HttpContextBase));
                httpContext.AddError(new FormsAuthenticationLogonException("Username or password is not correct."));
            }

            return user;
        }

        public TUser Logout()
        {
            FormsAuthentication.SignOut();
            return GetDefaultUser();
        }

        public void UpdateUser(TUser user)
        {
            throw new NotImplementedException();
        }
    }
}

namespace MainModule.Web
    {
        using System.ServiceModel.DomainServices.Hosting;
        // TODO: Create methods containing your application logic.
        [EnableClientAccess()]
        public class CustomAuthenticationService :FormsAuthenticationService<UserDTO>
        {
            protected override UserDTO GetCurrentUser(string name, string userData)
            {
                return new UserDTO {DisplayName = name, Name = name};
            }

            protected override UserDTO ValidateCredentials(string name, string password, string customData, out string userData)
            {
                userData = null;
                UserDTO user = null;


               if(name=="John" && password = "123")
               {
                    userData = name;
                    user =  new UserDTO {DisplayName = name, Email = "asdf"};

                 }
              retrurn user;
            }
        }
    }

这是我实施的类 - 它是在博客上发布的相同代码。没有例外,所以我无法粘贴stackTrace。我只是无法编译解决方案

2 个答案:

答案 0 :(得分:0)

确保使用正确的命名空间。

我注意到您粘贴的代码中有两个小错字:

  1. if(name=="John" && password = "123")
    应该是:
    if (name=="John" && password == "123")

  2. retrurn user;
    应该是:
    return user;

  3. 否则,它为我编译没有错误。

    1. 创建新的Web应用程序

    2. 添加对System.ServiceModel.DomainServices.Hosting的引用(例如来自“C:\ Program Files(x86)\ Microsoft SDKs \ RIA Services \ v1.0 \ Libraries \ Server \ System.ServiceModel.DomainServices.Hosting .dll“)

    3. 添加对System.ServiceModel.DomainServices.Server的引用(例如来自“C:\ Program Files(x86)\ Microsoft SDKs \ RIA Services \ v1.0 \ Libraries \ Server \ System.ServiceModel.DomainServices.Server .dll“)

    4. 创建一个名为CustomAuthenticationService的类并插入下面的代码。

      using System.ServiceModel.DomainServices.Hosting;
      using System.Web;
      using System.Web.Security;
      using System;
      using System.Security.Principal;
      using System.ServiceModel.DomainServices.Server;
      using System.ServiceModel.DomainServices.Server.ApplicationServices;
      
      namespace WebApplication1.Services
      {
          public class UserDTO : UserBase
          {
              public string DisplayName { get; set; }
              public string Email { get; set; }
          }
      
          public class FormsAuthenticationLogonException : System.Exception
          {
              public FormsAuthenticationLogonException(string message) : base(message) { }
          }
      
          // TODO: Create methods containing your application logic.
          [EnableClientAccess()]
          public abstract class FormsAuthenticationService<TUser> : DomainService, IAuthentication<TUser> where TUser : UserBase
          {
      
              protected abstract TUser GetCurrentUser(string name, string userData);
              protected abstract TUser ValidateCredentials(string name, string password, string customData, out string userData);
              protected virtual TUser GetDefaultUser()
              {
                  return null;
              }
      
              public TUser GetUser()
              {
                  IPrincipal currentUser = ServiceContext.User;
                  if ((currentUser != null) && currentUser.Identity.IsAuthenticated)
                  {
                      FormsIdentity userIdentity = currentUser.Identity as FormsIdentity;
                      if (userIdentity != null)
                      {
                          FormsAuthenticationTicket ticket = userIdentity.Ticket;
                          if (ticket != null)
                          {
                              return GetCurrentUser(currentUser.Identity.Name, ticket.UserData);
                          }
                      }
                  }
      
                  return GetDefaultUser();
              }
      
              public TUser Login(string userName, string password, bool isPersistent, string customData)
              {
                  string userData;
                  TUser user = ValidateCredentials(userName, password, customData, out userData);
      
                  if (user != null)
                  {
                      FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(/* version */ 1, userName,
                                                             DateTime.Now, DateTime.Now.AddMinutes(30),
                                                             isPersistent,
                                                             userData,
                                                             FormsAuthentication.FormsCookiePath);
      
                      string encryptedTicket = FormsAuthentication.Encrypt(ticket);
                      HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
      
                      HttpContextBase httpContext = (HttpContextBase)ServiceContext.GetService(typeof(HttpContextBase));
                      httpContext.Response.Cookies.Add(authCookie);
                  }
                  else
                  {
                      HttpContextBase httpContext = (HttpContextBase)ServiceContext.GetService(typeof(HttpContextBase));
                      httpContext.AddError(new FormsAuthenticationLogonException("Username or password is not correct."));
                  }
      
                  return user;
              }
      
              public TUser Logout()
              {
                  FormsAuthentication.SignOut();
                  return GetDefaultUser();
              }
      
              public void UpdateUser(TUser user)
              {
                  throw new NotImplementedException();
              }
          }
      
          // TODO: Create methods containing your application logic.
          [EnableClientAccess()]
          public class CustomAuthenticationService : FormsAuthenticationService<UserDTO>
          {
              protected override UserDTO GetCurrentUser(string name, string userData)
              {
                  return new UserDTO { DisplayName = name, Name = name };
              }
      
              protected override UserDTO ValidateCredentials(string name, string password, string customData, out string userData)
              {
                  userData = null;
                  UserDTO user = null;
      
      
                  if (name == "John" && password == "123")
                  {
                      userData = name;
                      user = new UserDTO { DisplayName = name, Email = "asdf" };
      
                  }
      
                  return user;
              }
          }
      }
      

答案 1 :(得分:0)

从FormsAuthenticationService抽象类中删除属性[EnableClientAccess()]。 它会编译没有任何错误