如何验证外部请求WCF Web服务? [英] How to authenticate external requests to WCF Web service?

查看:252
本文介绍了如何验证外部请求WCF Web服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过用户凭据的SOAP头传球来验证外部请求WCF Web服务。

 使用(UsrService客户端=新UsrService())
{
    client.Credentials =新System.Net.NetworkCredential(用户1,口令1);
    client.SomeRemoteMethod();
}

我得到异常:


  

未处理的异常:System.Net.WebException:请求失败,
  错误信息:对象移动


 未处理的异常:System.Net.WebException:请求失败,出现错误信息:
-
< HTML和GT;< HEAD><标题>对象移动< /标题>< /头><身体GT;
< H2>对象移动到< A HREF =/ NuiLogin.aspx RETURNURL =%2f0%2fServiceModel%2fSimp?
leCustomService.svc%2fsoap>此处&下; /一个方式>&下; / H2>
< /身体GT;< / HTML> - 。
   在System.Web.Services.Protocols.SoapHttpClientProtocol.ReadResponse(SoapClien
tMessage消息,WebResponse的响应,流responseStream,布尔asyncCall

   在System.Web.Services.Protocols.SoapHttpClientProtocol.Invoke(字符串methodNa
我,对象[]参数)
   在UsrService.SayHello()在C:\\ VS2015 \\项目\\ WCFSharedDLL \\ WCFSharedDLL \\ TestPr
oxyClass.cs:44行
   在ConsoleApplicationForTesting.Program.Main(字串[] args)在C:\\ VS2015 \\工程中的
TS \\ ConsoleApplicationForTesting \\ ConsoleApplicationForTesting \\的Program.cs:行1
6

如何可以验证外部请求WCF Web服务?

我将是十分感激。感谢所有。


解决方案

再一看下面的方式。

通过使用传输协议我给用户凭据如下:

 公共常量字符串authServiceUri =HTTP://本地主机:8080 / ServiceModel / AuthService.svc /登录;
公共静态的CookieContainer AuthCookie =新的CookieContainer();公共静态布尔TryLogin(用户名字符串,字符串的userPassword)
{
    VAR authRequest = HttpWebRequest.Create(authServiceUri)为HttpWebRequest的;
    authRequest.Method =POST;
    authRequest.ContentType =应用/ JSON;
    authRequest.CookieContainer = AuthCookie;    使用(VAR requesrStream = authRequest.GetRequestStream())
    {
        使用(VAR作家=新的StreamWriter(requesrStream))
        {
            writer.Write(@{
                用户名:,+使用者名称+ @
                的userpassword:,+ +的userPassword @
            });
        }
    }    使用(VAR响应=(HttpWebResponse)authRequest.GetResponse())
    {
        如果(AuthCookie.Count大于0)
        {
            返回true;
        }
    }
    返回false;
}

然后我设置的CookieContainer如下:

  client.CookieContainer = AuthCookie;

这之后就有可能访问Web - 服务:

 静态无效的主要(字串[] args)
{
    使用(UsrService客户端=新UsrService())
    {
        如果(TryLogin(用户1,用户1))
        {
            client.CookieContainer = AuthCookie;
            Console.WriteLine(client.SayHello());
        }
    }
}

在这里输入的形象描述

虽然目前还不清楚为什么我不能通过凭据的SOAP头的传递验证请求......

I'm trying to authenticate external requests to WCF Web service through the passing of user credentials in the SOAP header.

using (UsrService client = new UsrService())
{
    client.Credentials = new System.Net.NetworkCredential("User 1", "Password 1");
    client.SomeRemoteMethod();
}

I get the exception:

Unhandled exception: System.Net.WebException: The request failed with the error message: Object moved

Unhandled exception: System.Net.WebException: The request failed with the error message:
--
<html><head><title>Object moved</title></head><body>
<h2>Object moved to <a href="/NuiLogin.aspx?ReturnUrl=%2f0%2fServiceModel%2fSimp
leCustomService.svc%2fsoap">here</a>.</h2>
</body></html>

--.
   in System.Web.Services.Protocols.SoapHttpClientProtocol.ReadResponse(SoapClien
tMessage message, WebResponse response, Stream responseStream, Boolean asyncCall
)
   in System.Web.Services.Protocols.SoapHttpClientProtocol.Invoke(String methodNa
me, Object[] parameters)
   in UsrService.SayHello() in c:\VS2015\Projects\WCFSharedDLL\WCFSharedDLL\TestPr
oxyClass.cs:line 44
   in ConsoleApplicationForTesting.Program.Main(String[] args) in c:\VS2015\Projec
ts\ConsoleApplicationForTesting\ConsoleApplicationForTesting\Program.cs:line 1
6

How can I authenticate external requests to WCF Web service?

I would be very grateful for the information. Thanks to all.

解决方案

Made in the following way.

By using transport protocol I sent user credentials as follows:

public const string authServiceUri = "http://localhost:8080/ServiceModel/AuthService.svc/Login";
public static CookieContainer AuthCookie = new CookieContainer();

public static bool TryLogin(string userName, string userPassword)
{
    var authRequest = HttpWebRequest.Create(authServiceUri) as HttpWebRequest;
    authRequest.Method = "POST";
    authRequest.ContentType = "application/json";
    authRequest.CookieContainer = AuthCookie;

    using (var requesrStream = authRequest.GetRequestStream())
    {
        using (var writer = new StreamWriter(requesrStream))
        {
            writer.Write(@"{
                ""UserName"":""" + userName + @""",
                ""UserPassword"":""" + userPassword + @"""
            }");
        }
    }

    using (var response = (HttpWebResponse)authRequest.GetResponse())
    {
        if (AuthCookie.Count > 0)
        {
            return true;
        }
    }
    return false;
}

Then I set CookieContainer as follows:

client.CookieContainer = AuthCookie;

After that it became possible to access the Web - service:

static void Main(string[] args)
{
    using (UsrService client = new UsrService())
    {
        if(TryLogin("User 1", "User 1"))
        {
            client.CookieContainer = AuthCookie;
            Console.WriteLine(client.SayHello());
        }
    }
}

Although it is unclear why I cannot authenticate the requests via the passing of credentials in the SOAP header...

这篇关于如何验证外部请求WCF Web服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆