如何验证对 WCF Web 服务的外部请求?
How to authenticate external requests to WCF Web service?
我正在尝试通过在 SOAP 中传递用户凭据来验证对 WCF Web 服务的外部请求 header。
using (UsrService client = new UsrService())
{
client.Credentials = new System.Net.NetworkCredential("User 1", "Password 1");
client.SomeRemoteMethod();
}
我得到异常:
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
如何验证对 WCF Web 服务的外部请求?
按以下方式制作。
通过使用传输协议,我按如下方式发送了用户凭据:
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;
}
然后我设置CookieContainer如下:
client.CookieContainer = AuthCookie;
之后可以访问 Web - 服务:
static void Main(string[] args)
{
using (UsrService client = new UsrService())
{
if(TryLogin("User 1", "User 1"))
{
client.CookieContainer = AuthCookie;
Console.WriteLine(client.SayHello());
}
}
}
我正在尝试通过在 SOAP 中传递用户凭据来验证对 WCF Web 服务的外部请求 header。
using (UsrService client = new UsrService())
{
client.Credentials = new System.Net.NetworkCredential("User 1", "Password 1");
client.SomeRemoteMethod();
}
我得到异常:
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
如何验证对 WCF Web 服务的外部请求?
按以下方式制作。
通过使用传输协议,我按如下方式发送了用户凭据:
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;
}
然后我设置CookieContainer如下:
client.CookieContainer = AuthCookie;
之后可以访问 Web - 服务:
static void Main(string[] args)
{
using (UsrService client = new UsrService())
{
if(TryLogin("User 1", "User 1"))
{
client.CookieContainer = AuthCookie;
Console.WriteLine(client.SayHello());
}
}
}