2010-09-30 104 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);