授权才能使用的WebService

首先我给 WebService添加一个名为Login的方法: 
[WebMethod(EnableSession  =   true )]
public   string  Login( string  userName,  string  password)
{
    
if  (userName  ==   " username "   &&  password  ==   " password " )
    {
        Session[
" login " =   1 ;
        
return   " welcome " ;
    }
    
else
        
return   " login failed " ;
}

好了,服务器端的工作就查不多了(这么简单,不可思议吧),为了检验我们这个方法是否有效,我们再添加一个方法验证一下:
[WebMethod(EnableSession  =   true )]
public   string  GetStatus()
{
    
if  (Convert.ToInt32(Session[ " login " ])  ==   1 )
        
return   " Thank you. " ;
    
else
        
return   " Please Login first. " ;
}

调用:
using  System;
using  System.Data;
using  System.Data.SqlClient;
using  System.Net;
using  TestCon.login;

class  MainEntryPoint
{
    
static   void  Main( string [] args)
    {
        LoginService ls 
=   new  LoginService();
        CookieContainer cc 
=   new  CookieContainer();
        ls.CookieContainer 
=  cc;
        Console.WriteLine(ls.Login(
" username " " password " ));
        Console.ReadLine();
        Console.WriteLine(ls.GetStatus());
    }
}
 
再Ctrl+F5,变:

你可能感兴趣的:(授权才能使用的WebService)