C# - HttpWebResponse redirect to external URL -
i'm trying achieve following:
- i'm building mvc website me automatically logon site.
- websitea call websiteb using httpwebrequest.
- websitea send details of user through headers (request.headers.add)
- websiteb handle user authentication , internally redirect page granting access user.
i've managed achieve part of i'm stuck in displaying redirection return. knows if can achieved?
here code gets called in websitea app:
[httppost] [outputcache(nostore = true, duration = 0, varybyparam = "*")] public actionresult login(mymodel model) { httpwebrequest request = (httpwebrequest)webrequest.create("http://websiteb/login.aspx"); request.allowautoredirect = true; request.method = "post"; request.headers.add("myusertologon", model.user); //i'm sending user through headers string postdata = "this test"; byte[] bytearray = encoding.utf8.getbytes(postdata); request.contenttype = "application/x-www-form-urlencoded"; request.contentlength = bytearray.length; stream datastream = request.getrequeststream(); datastream.write(bytearray, 0, bytearray.length); datastream.close(); //get response login page httpwebresponse response = (httpwebresponse)request.getresponse(); // display status. console.writeline(((httpwebresponse)response).statusdescription); datastream = response.getresponsestream(); streamreader reader = new streamreader(datastream); string responsefromserver = reader.readtoend(); console.writeline(responsefromserver); reader.close(); datastream.close(); response.close(); //what here? want go redirected page websiteb/page1.aspx //see code below websiteb. return null; }
in websiteb have code redirection when user logged in:
....... users = request.querystring["myusertologon"]; ....... private void login(string user) { grantuser(user, session.sessionid); httpcookie mycookie = new httpcookie("test"); mycookie .expires = datetime.now.addminutes(10); response.cookies.add(mycookie); response.addheader("mycookie ", mycookie .value); response.redirect("websiteb/page1.aspx"); }
any appreciated.
i had same problem sites.
you can't use redirect because done iis , never receive authentication cookies websiteb.
in case returning view instead of redirect worked. , redirect on websitea after setting cookies
also seems need add cookie container request cookies back.
here may code
landing site
[httppost] public actionresult login(string accountid) { var url = "http://...." var request = (httpwebrequest)webrequest.create(url + "/account/wamlogin/"); request.headers.add("user", accountid); request.cookiecontainer = new cookiecontainer(); var response = (httpwebresponse)request.getresponse(); foreach (cookie cook in response.cookies) { response.cookies.add(new httpcookie(cook.name, cook.value) { expires = cook.expires, httponly = cook.httponly, domain = cook.domain }); } return new redirectresult(url); }
here websiteb code
[allowanonymous] public virtual actionresult wamlogin(string returnurl, string id) { var accountid= request.headers["user"]; ..... return view(mvc.account.views.login); }
hope you
Comments
Post a Comment