2012-03-24 13 views
-3

私はWebサービスをWebサービスに作成するつもりです.Clientはユーザー名とパスワードを送信します.Webメソッド、つまりIISレベルに到達する前にユーザーを認証したいと思います。誰もがコードやリンクを共有することができます。ユーザーの詳細はMSSQLデータベースに保存されています[このようなユーザーはDatabase.Ifの既存のものであることを検証したいですが、ユーザーが存在しない場合は、目的はすべてのWebメソッドでユーザーの検証を避けることです]。WebServiceで認証を実装する方法

+5

"誰がplsはコードやリンクを共有することができます" を試してみてください本当の質問に戻ってきますか? –

答えて

5

RESTベースのWebサービスを構築し、ユーザー名とパスワードではなくAPIキーを使用して送信することをお勧めします。それを実現する方法についてのチュートリアルのヒープのためREST GET requests, verbs and apikey

それからちょうどGoogleのRESTベースのASP.NETウェブサービス:

はを見てください。私は、元のページを見つけることができませんが、このコードは、どこかのサンプルをオフに基づいていた:実装

注意を示すように更新

ステップ1 - [編集] web.configファイル

system.serviceModelセクションでこれを挿入します。

<behaviors> 
    <serviceBehaviors> 
    <clear /> 
    <behavior> 
     <!-- This behavior enables API Key Verification --> 
     <serviceAuthorization serviceAuthorizationManagerType="API.APIKeyAuthorization, API" /> 
     <serviceMetadata httpGetEnabled="true" /> 
    </behavior> 
    </serviceBehaviors> 
</behaviors> 

ステップ2 - 変更

あなたの実際の状況に合わせてIsValidAPIキーのコードをAPI認可のクラスを作成します。あなたはAPIキーが有効かどうかをテストする関数にmineが行くのを見ることができます。

namespace API 
{ 
public class APIKeyAuthorization : ServiceAuthorizationManager 
{ 
    public const string APIKEY = "APIKey"; 
    public const string APIKEYLIST = "APIKeyList"; 

    protected override bool CheckAccessCore(OperationContext operationContext) 
    { 
     return IsValidAPIKey(operationContext); 
    } 

    public bool IsValidAPIKey(OperationContext operationContext) 
    { 
     // if verification is disabled, return true 
     if (Global.APIKeyVerification == false) 
      return true; 

     string key = GetAPIKey(operationContext); 

     // Convert the string into a Guid and validate it 
     if (BusinessLogic.User.ApiKey.Exists(key)) 
     { 
      return true; 
     } 
     else 
     { 
      // Send back an HTML reply 
      CreateErrorReply(operationContext, key); 
      return false; 
     } 
    } 

    public string GetAPIKey(OperationContext operationContext) 
    { 
     // Get the request message 
     var request = operationContext.RequestContext.RequestMessage; 

     // Get the HTTP Request 
     var requestProp = (HttpRequestMessageProperty)request.Properties[HttpRequestMessageProperty.Name]; 

     // Get the query string 
     NameValueCollection queryParams = HttpUtility.ParseQueryString(requestProp.QueryString); 

     // Return the API key (if present, null if not) 
     return queryParams[APIKEY]; 
    } 

    private static void CreateErrorReply(OperationContext operationContext, string key) 
    { 
     // The error message is padded so that IE shows the response by default 
     using (var sr = new StringReader("<?xml version=\"1.0\" encoding=\"utf-8\"?>" + APIErrorHTML)) 
     { 
      XElement response = XElement.Load(sr); 
      using (Message reply = Message.CreateMessage(MessageVersion.None, null, response)) 
      { 
       HttpResponseMessageProperty responseProp = new HttpResponseMessageProperty() { StatusCode = HttpStatusCode.Unauthorized, StatusDescription = String.Format("'{0}' is an invalid API key", key) }; 
       responseProp.Headers[HttpResponseHeader.ContentType] = "text/html"; 
       reply.Properties[HttpResponseMessageProperty.Name] = responseProp; 
       operationContext.RequestContext.Reply(reply); 
       // set the request context to null to terminate processing of this request 
       operationContext.RequestContext = null; 
      } 
     } 
    } 

    public const string APIErrorHTML = @" 
<html> 
<head> 
    <title>Request Error - No API Key</title> 
    <style type=""text/css""> 
     body 
     { 
      font-family: Verdana; 
      font-size: x-large; 
     } 
    </style> 
</head> 
<body> 
    <h1> 
     Request Error 
    </h1> 
    <p> 
     A valid API key needs to be included using the apikey query string parameter 
    </p> 
</body> 
</html> 
"; 
    } 
} 

あなたはウェブメソッドにヒットする前にAPI権限が設定されています。ただし、この例では、クエリ文字列として渡されるAPIキーを示しています。上記のリンクの例を参考にして、これをHTTPヘッダーで送信するように変更してください。より洗練されたアプローチでは、あなたのURLを駄目にしません。しかし、それは皆あなたの必要条件に依存します。

を更新 は、実は私はこの出くわし: - :どのようにあなたが書くについていくつかのMicrosoft ASP.NETのWeb API http://www.asp.net/web-api

その:)

+0

疑問は、webmethodに届くリクエストを処理する方法です。 – user922834

関連する問題