开发者

MessageSecurityException, UsernamePasswordValidator, and HttpWebRequest Problem

is there a better way to return back the standard "Invalid username/password" message to clients that connect to a WCF service requiring credentials? I have the following code, which causes an exception when I connect to my WCF service with invalid usernames/passwords.

Ideally I'd like back just a simple string, but to get the UsernamePasswordValidator to signal bad usernames requires throwing either a FaultException or SecurityTokenException, which causes an exception on the client-side at the HttpWebRequest's GetResponse method.

FileInfo file = new FileInfo("Request.xml");
string payload = file.OpenText().ReadToEnd();

HttpWebRequest request = HttpWebRequest.Create(URL) as HttpWebRequest;
request.ContentType = "text/xml";
request.ContentLength = payload.Length;
request.MediaType = "utf-8";
request.Method = "POST";
StreamWriter writer = new StreamWriter(request.GetRequestStream());
writer.Write(payload);
writer.Flush();
writer.Close();

Console.WriteLine("Awaiting Response...");
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
StreamReader reader = new StreamReader(response.GetResponseStream());
Console.WriteLine(reader.ReadToEnd());
Console.ReadKey(true);

The reason I want to do t开发者_C百科hat is the service will be hit by clients not using .NET.


Put it in a try - catch statement and catch the exception, then pass your own message in the catch block

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜