0

ASP.NET Web サイト内でホストされていた WCF サービスを取得しました。このサービスは、誰かがその Web サイトにログインした場合にのみ利用できます。WCF サービスは webHttp バインディングを使用します。ここで、他の M/c のコンソール アプリケーションからサービスを利用する必要があります。Web サイトにログインするための認証資格情報 (ユーザー名/パスワード) を持っています。資格情報を使用してコンソール アプリケーションからサービスを利用する方法はありますか? 前提条件として、サービス側では何も変更できません。

4

1 に答える 1

0

フォーム認証を使用するページの背後にあるコードが、実際のログイン処理を行うために WCF サービスを呼び出していたため、この手法はうまくいきましたOperationContract

    string address = "http://localhost:4567";
    var serviceClient = new SomeService.MyServiceClient(
        "BasicHttpBinding_IMyService", address + "/myservice.svc");

    // Call the service login method, save off the response's cookie(s)
    string cookiesFromLogin = string.Empty;
    bool logonSucceeded = false;
    using (new OperationContextScope(serviceClient.InnerChannel))
    {
        logonSucceeded = serviceClient.DoTheLogin("userName", "userPass");

        var httpResponse = (HttpResponseMessageProperty)
            OperationContext.Current.IncomingMessageProperties[
                HttpResponseMessageProperty.Name];
        cookiesFromLogin = httpResponse.Headers["Set-Cookie"];
    }

    // Then later when you need to make other service calls, insert the cookie(s)
    // into the request headers
    using (new OperationContextScope(serviceClient.InnerChannel))
    {
        var httpRequest = new HttpRequestMessageProperty();
        httpRequest.Headers["Cookie"] = cookiesFromLogin;
        OperationContext.Current.OutgoingMessageProperties[
            HttpRequestMessageProperty.Name] = httpRequest;

        var someResults = serviceClient.CallSomeMethod("param1");
    }
于 2013-08-07T01:21:25.820 に答える