8

実行しようとしている基本的な ASMX サービスがあります (WCF を使用したいのですが、サーバーで動作させることができません)。セキュリティなしのセットアップでは問題なく動作しますが、セキュリティをオンにするとすぐに次のようになります。

HTTP 要求は、クライアント認証スキーム「匿名」では許可されていません。サーバーから受信した認証ヘッダーは、「基本レルム="セキュア領域"」でした。

私が欲しいのは、ユーザーに名前とパスワードの種類の解決策を最小限に求めることです。

IntelliSense でコードを調べてみても、必要と思われるものは何も見つかりません。

これは便利なように見えますが、WCF のようです。


これをライブデモにできることに気づきました:

ここにサービスがあります:http://smplsite.com/sandbox3/Service1.asmx

ユーザー名はtestappで、パスワードはtestpwです。そのサービスで関数を呼び出すコマンド ライン アプリが必要です。

セキュリティを追加する前に、この行はAdd Web Service Referenceその URL で実行した後、基本的な VS プロジェクトで機能しました

new ServiceReference1.Service1SoapClient().HelloMom("Bob");

これは私の現在の試みです(うまくいきません)

class Program
{
    private static bool customValidation(object s, X509Certificate c, X509Chain ch, SslPolicyErrors e)
    { return true }

    static void Main(string[] args)
    {
         // accept anything
        ServicePointManager.ServerCertificateValidationCallback += 
              new RemoteCertificateValidationCallback(customValidation);

        var binding = new BasicHttpBinding(BasicHttpSecurityMode.Transport);
        binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
        binding.Security.Transport.Realm = "Secured area";

        // the generated Web Service Reference class
        var client = new ServiceReference1.Service1SoapClient(
            binding,
            new EndpointAddress("https://smplsite.com/sandbox3/Service1.asmx")
            );

        client.ClientCredentials.UserName.UserName = "testapp";
        client.ClientCredentials.UserName.Password = "testpw";

        Console.WriteLine(client.HelloMom("Bob"));
    }
}

編集:ところで、これはウェブサイトでもブラウザで実行されているものでもありません。アクセスコードはC# command line app です。また、認証は、私が制御していない別の IIS プラグインによって行われています。

編集2:明確にするために。私が探している解決策は、純粋にクライアント側の問題です。

編集 3:アクセス制御は.haccess一種のシステムを介して行われ、その方法が気に入っています。サービスコードで認証を行いたくありません。

4

3 に答える 3

12

編集:
これを使用するのはどうですか:

MyWebService svc = new MyWebService();            
svc.Credentials = new System.Net.NetworkCredential(UserID, pwd);
bool result = svc.MyWebMethod();    

OPはこれはうまくいかないと言っていますが、今では彼の状況ではうまくいかないことがわかりました。

次のようにします。

public class MyWebService : System.Web.Services.WebService
{
    public AuthenticationHeader AuthenticationInformation;

    public class AuthenticationHeader : SoapHeader
    {
        public string UserName;
        public string Password;
    }

    [WebMethod( Description = "Sample WebMethod." )]
    [SoapHeader( "AuthenticationInformation" )]
    public bool MyWebMethod()
    {
        if ( AuthenticationInformation != null )
        {
            if ( IsUserAuthenticated( AuthenticationInformation.UserName,   
                 AuthenticationInformation.Password, ref errorMessage ) )
            {
                 // Authenticated, do something
            }
            else
            {
                 // Failed Authentication, do something
            } 
        }
        else
        {
                 // No Authentication, do something
        }
    }
}

IsUserAuthenticated() を提供することに注意してください。

次に、クライアントは次のように呼び出します。

 MyWebService svc = new MyWebService();            
 svc.AuthenticationHeaderValue = new MyWebService.AuthenticationHeader();
 svc.AuthenticationHeaderValue.UserName = UserID;
 svc.AuthenticationHeaderValue.Password = Password;

 bool result = svc.MyWebMethod();
于 2009-04-20T23:00:46.623 に答える
2

これが役立つと思うので、新しい回答を追加します。

http://intellitect.com/calling-web-services-using-basic-authentication/

グーグル以外は何もしていないので、ここでは複製しません。

于 2009-04-22T14:27:41.550 に答える
2

構成:

<binding name="MyBinding" closeTimeout="00:00:30"
      openTimeout="00:00:30" receiveTimeout="00:00:30" sendTimeout="00:00:30"
      allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
      maxBufferPoolSize="524288" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647"
      textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true"
      messageEncoding="Text">
      <readerQuotas maxDepth="32" maxStringContentLength="2147483647"
        maxArrayLength="2147483647" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
      <security mode="Transport">
        <transport clientCredentialType="Basic" realm=""/>
      </security>
    </binding>
  </basicHttpBinding>

消費しながら

proxy.ClientCredentials.UserName.UserName = userName;
proxy.ClientCredentials.UserName.Password = password;

これは私にとってはうまくいきました。

于 2013-03-18T18:28:42.140 に答える