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

Authentication 自定义身份验证域服务-Silverlight和RIA,authentication,silverlight-4.0,forms-authentication,ria,Authentication,Silverlight 4.0,Forms Authentication,Ria,我正在尝试编写自定义身份验证域服务。我想我理解了上面写的所有代码 但是,我不知道如何指定应该使用哪个域服务应用程序。我有一个抽象域服务,第二个是该服务的具体实现。如果我构建整个解决方案,我会得到一个错误 'MainModule.Web.FormsAuthenticationService`1' is not a valid DomainService type. DomainService types cannot be abstract or generic. 我没有在我之前提到的博客上找到

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

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

'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;
            }
        }
    }
namespace MainModule.Web
{
使用制度;
使用System.ServiceModel.DomainServices.Hosting;
使用System.ServiceModel.DomainServices.Server;
//TODO:创建包含应用程序逻辑的方法。
[EnableClientAccess()]
公共抽象类FormsAuthenticationService:DomainService,IAAuthentication,其中TUser:UserBase
{
受保护的抽象TUser GetCurrentUser(字符串名称、字符串用户数据);
受保护的抽象TUser ValidateCredentials(字符串名称、字符串密码、字符串customData、out字符串userData);
受保护的虚拟TUser GetDefaultUser()
{
返回null;
}
公共TUser GetUser()
{
IPrincipal currentUser=ServiceContext.User;
if((currentUser!=null)&¤tUser.Identity.IsAuthenticated)
{
FormsIdentity userIdentity=currentUser.Identity作为FormsIdentity;
if(userIdentity!=null)
{
FormsAuthenticationTicket票证=userIdentity.ticket;
如果(票证!=null)
{
返回GetCurrentUser(currentUser.Identity.Name,ticket.UserData);
}
}
}
返回GetDefaultUser();
}
公共用户登录(字符串用户名、字符串密码、bool ispersist、字符串customData)
{
字符串用户数据;
TUser user=ValidateCredentials(用户名、密码、customData、out userData);
如果(用户!=null)
{
FormsAuthenticationTicket票证=新的FormsAuthenticationTicket(/*版本*/1,用户名,
DateTime.Now,DateTime.Now.AddMinutes(30),
持久的,
用户数据,
FormsAuthentication.FormScookePath);
字符串encryptedTicket=FormsAuthentication.Encrypt(票证);
HttpCookie authCookie=新的HttpCookie(FormsAuthentication.FormScookeName,encryptedTicket);
HttpContextBase httpContext=(HttpContextBase)ServiceContext.GetService(typeof(HttpContextBase));
httpContext.Response.Cookies.Add(authCookie);
}
其他的
{
HttpContextBase httpContext=(HttpContextBase)ServiceContext.GetService(typeof(HttpContextBase));
httpContext.AddError(新表单身份验证LogonException(“用户名或密码不正确”);
}
返回用户;
}
公共TUser注销()
{
FormsAuthentication.SignOut();
返回GetDefaultUser();
}
public void UpdateUser(TUser用户)
{
抛出新的NotImplementedException();
}
}
}
名称空间MainModule.Web
{
使用System.ServiceModel.DomainServices.Hosting;
//TODO:创建包含应用程序逻辑的方法。
[EnableClientAccess()]
公共类CustomAuthenticationService:FormsAuthenticationService
{
受保护的覆盖UserDTO GetCurrentUser(字符串名称、字符串userData)
{
返回新的UserDTO{DisplayName=name,name=name};
}
受保护的重写UserdToValidateCredentials(字符串名称、字符串密码、字符串customData、out字符串userData)
{
userData=null;
UserDTO user=null;
如果(name==“John”&&password=“123”)
{
userData=名称;
user=newuserdto{DisplayName=name,Email=“asdf”};
}
回收用户;
}
}
}

这是我实现的类——这和我在博客上发布的代码是一样的。没有例外,因此我无法粘贴stackTrace。我无法编译解决方案

请确保您使用了正确的名称空间

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

  • if(name==“John”&&password=“123”)

    应该是:
    if(name==“John”和&password==“123”)

  • retrurn用户
    应该是:
    返回用户

  • 否则,我编译时不会出错

  • 创建新的Web应用程序

  • 添加对
    System.ServiceModel.DomainServices.Hosting
    (例如来自“C:\Program Files(x86)\Microsoft SDK\RIA Services\v1.0\Libraries\Server\System.ServiceModel.DomainServices.Hosting.dll”)

  • 添加对
    System.ServiceModel.DomainServices.Server
    (例如“C:\Program Files(x86)\Microsoft SDK\RIA Services\v1.0\Libraries\Server\System.ServiceModel.DomainServices.Server.dll”)

  • 创建一个名为
    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;
            }
        }
    }
    
    使用System.ServiceModel.DomainServices.Hosting;
    使用System.Web;
    使用System.Web.Security;
    使用制度;
    使用System.Security.Principal;
    使用System.ServiceModel.DomainServices.Server;
    使用System.ServiceModel.DomainServices.Server.ApplicationServices;
    命名空间WebApplication1.Services
    {
    公共类UserDTO:UserBase
    {
    公共字符串DisplayName{get;set;}
    公共字符串电子邮件{get;set