2010-09-30 126 views
1

我是webservices的新手.. 我有一個Silverlight 4應用程序,創建了一個登錄頁面。 我創建了一個Web服務:Silverlight 4中的身份驗證Webservice

MagNET.AuthenticationService.AuthenticationServiceSoapClient svc = 
    new MagNET.AuthenticationService.AuthenticationServiceSoapClient(); 
svc.HelloWorldCompleted += new EventHandler<AuthenticationService.HelloWorldCompletedEventArgs>(svc_HelloWorldCompleted); 
svc.HelloWorldAsync(); 


void svc_HelloWorldCompleted(object sender, AuthenticationService.HelloWorldCompletedEventArgs e) 
{ 
    MessageBox.Show("User logged in"); 
} 

我不知道如何來調用Web服務的authenticateUser方法(:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Services; 
using System.Web.Security; 

namespace MagNET.Web.WebServices 
{ 
    /// <summary> 
    /// Summary description for AuthenticationService 
    /// </summary> 
    [WebService(Namespace = "http://tempuri.org/")] 
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
    [System.ComponentModel.ToolboxItem(false)] 
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService] 
    public class AuthenticationService : System.Web.Services.WebService 
    { 

     [WebMethod(EnableSession = true)] 
     public bool AuthenticateUser(string username, string password) 
     { 
      //bool valid = Membership.ValidateUser(username, password); 
      bool valid = true; 

      if (valid) 
      { 
       FormsAuthentication.SetAuthCookie(username, false); 
       Session["username"] = username; 
      } 
      else 
      { 
       Session["username"] = null; 
      } 

      return valid; 
     } 

     [WebMethod(EnableSession = true)] 
     public string HelloWorld() 
     { 
      if (!IsLoggedIn()) 
      { 
       throw new Exception("User is not logged in!!!"); 
      } 
      else 
      { 
       return "Hello World!"; 
      } 
     } 

     private bool IsLoggedIn() 
     { 
      if (Session["username"] != null) 
       return true; 
      else 
       return false; 
     } 

    } 
} 

我使用此代碼,以找出是否用戶登錄我不知道如何在登錄按鈕從我的登錄頁面被按下時有效登錄用戶)。

回答

1

只需使用authenticateUser方法代替HelloWorld的方法:

MagNET.AuthenticationService.AuthenticationServiceSoapClient svc = 
     new MagNET.AuthenticationService.AuthenticationServiceSoapClient(); 
    svc.AuthenticateUserCompleted += new EventHandler<AuthenticationService.AuthenticateUserCompletedEventArgs>(svc_AuthenticateUserCompleted); 
    string username = txtBoxUsername.Text; 
    string password = txtBoxPassword.Password; 
    svc.AuthenticateUserAsync(username, password); 
相關問題