0

사용자 지정 인증 도메인 서비스를 쓰려고합니다. 필자는 this blog에 작성된 모든 코드를 이해했다고 생각합니다.사용자 지정 인증 도메인 서비스 - Silverlight 및 RIA

그러나 도메인 서비스 응용 프로그램을 사용해야하는 방법을 지정하는 방법을 모르겠습니다. 나는 하나의 추상 도메인 서비스를 가지고 있고 두 번째 것은이 서비스의 구체적인 구현이다. 전체 솔루션을 구축하면 오류가 발생합니다.

'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를 붙여 넣을 수 없습니다. 방금 솔루션을 컴파일 할 수 없습니다.

+0

오류 메시지의 코드 및 전체 스택 추적을 봅니다. –

+0

@ 라미. 내 질문을 편집했습니다. stacktrace를 붙여 넣을 수 없도록 솔루션을 빌드 할 수 없다는 사실을 알립니다. – John

+0

컴파일러 오류를 가리키는 행은 무엇입니까? –

답변

0

올바른 네임 스페이스를 사용하고 있는지 확인하십시오.

  1. if(name=="John" && password = "123")
    가되어야한다 :
    if (name=="John" && password == "123")

  2. retrurn user;
    가되어야한다
    return user;

  3. 내가 붙여 넣은 코드에서 두 개의 작은 오타를 발견

그렇지 않으면 오류없이 컴파일됩니다.

  1. 새 웹 응용 프로그램

  2. System.ServiceModel.DomainServices.Hosting (예에 대한 참조를 추가 C "에서 만듭니다. \ 마이크로 소프트의 SDK \ RIA 서비스 \ Program 파일 (x 86) \ V1.0 \ 라이브러리 \ 서버 \를 System.ServiceModel.DomainServices.Hosting.dll ")는

  3. System.ServiceModel.DomainServices.Server에 대한 참조 추가 (예를에서."C : \ Program 파일 (x 86) \ 마이크로 소프트의 SDK \ RIA 서비스 \ V1.0 \ 라이브러리 \ 서버 \ 시스템 .ServiceModel.DomainServices.Server.dll ")

  4. CustomAuthenticationService 클래스를 만들고 아래 코드를 삽입하십시오. FormsAuthenticationService 추상 클래스에서

    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; 
         } 
        } 
    } 
    
0

제거 속성 [EnableClientAccess()]. 오류없이 컴파일됩니다.