问题
I use webclient to call a web service, I pass username & password as basic authorizaiton in webclient header. The request is redirected to another URL, but basic authorizaiton info is not included in the re-directed request header, so I get 401 not authorized error. So how to keep authorization info in request header when redirect? thanks I notice the authorization is actually carried in cookies
I am using C# , .NEt 4.0, VS2010
回答1:
webclient strips out your customer authentication headers, and for good reason! If you get redirected to who knows where?, you certainly don't want to be sending them your authentication information!
If you are getting redirected to somewhere in the same directory (I think?) then you can use
Request.PreAuthenticate = true
but odds are that you will need to set up your program to handle the redirect manually, and then use a webclient request to connect to the correct server.
来源:https://stackoverflow.com/questions/9846408/webclient-how-to-keep-basic-authorization-when-redirect