authentication - How to authenticate external requests to WCF Web service? -
i'm trying authenticate external requests wcf web service through passing of user credentials in soap header.
using (usrservice client = new usrservice()) { client.credentials = new system.net.networkcredential("user 1", "password 1"); client.someremotemethod(); }
i exception:
unhandled exception: system.net.webexception: request failed error message: object moved
unhandled exception: system.net.webexception: request failed error message: -- <html><head><title>object moved</title></head><body> <h2>object moved <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 authenticate external requests wcf web service?
i grateful information. all.
made in following way.
by using transport protocol sent user credentials 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) 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 set cookiecontainer follows:
client.cookiecontainer = authcookie;
after became possible access 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 unclear why cannot authenticate requests via passing of credentials in soap header...
Comments
Post a Comment