1

I just want to ask how can i use HttpRequest in passing a parameter. We use Header for storing the parameter.

This is used to support Single Sign On function.

Thanks in advance! :)

VimariE
  • 31
  • 1
  • 5

1 Answers1

1

You can use the Request object in your C# mvc controller:

Request.Header.Add(string name, string value); - adds a parameter to the header Request.Header.GetKey(string name); - access parameter entered into the header

I hope this helps out, and answers your question since it was a little unclear regarding type of technology you want to use

Edit - New Information about problem

HttpContext context = HttpContext.Current;
string username = context.Request.Headers["username"].ToString();
string password = context.Request.Headers["password"].ToString();

This can serve as a good replacement for the helper function

audiochick
  • 184
  • 1
  • 10
  • Hi, can you give a more detailed example? sorry, i'm a newbie in this. :) – VimariE Feb 26 '14 at 09:22
  • For example, in your controller you can just use the Request variable straight. So if you want to get the username and password from the header, use `var username = Request.Header.GetKey('username');`..etc, provided your username is called username in the header. If you are still confused about using headers for single sign on, consider some other methods, for example using sessions on a separate server to save info, or using browser cookies to save login status, or even a central user table (it all depends on your system). – audiochick Feb 26 '14 at 09:25
  • i'll try it now. thanks a lot. unfortunately, i can't use other options since the code for single sign on is already finished and i just need to simulate it for testing. – VimariE Feb 26 '14 at 09:39
  • Request.Headers.Add("sm_eid", "12345"); System.Diagnostics.Debug.WriteLine(Request.Headers.GetKey("sm_eid"); i get an error in Request.Headers.GetKey("sm_eid") saying the Getkey accepts integers. – VimariE Feb 26 '14 at 09:51
  • I've updated my answer with a possible solution. I hope this sorts it out – audiochick Feb 26 '14 at 10:04
  • hi, ahm. what should i pass in the second parameter? and also, i got an error in Request.Headers.Add("sm_eid", "12345"); saying operation is not supported in this platform. thanks! :) – VimariE Feb 26 '14 at 10:12
  • And also there is an error in the Request request arguments in your function. – VimariE Feb 26 '14 at 10:24
  • Well, that's because I was writing more psuedo code, and have not tried to build it at all. My intention was to give you an idea of how you could solve your problem. The Request in the function is most likely of type HttpRequestContext, and so your function call in the controller should be `string[] values = getHeaderValue('key', Request);` – audiochick Feb 26 '14 at 10:36
  • Regarding your add function error: try `HttpContextFactory.Current.Request.Headers.Add(key, value);` – audiochick Feb 26 '14 at 10:41
  • Check out the updated answer. If you are attempting all of this in a unit test, I suggest you look at http://stackoverflow.com/questions/9624242/setting-the-httpcontext-current-session-in-unit-test/18982320#18982320 – audiochick Feb 26 '14 at 10:43
  • Pardon but i really can't follow. Let me try your new suggestion. Thanks! – VimariE Feb 26 '14 at 10:45
  • Operation is not supported on this platform. I got this error in HttpContextFactory.Current.Request.Headers.Add(key, value); – VimariE Feb 26 '14 at 11:03
  • Have you tried setting it like this: `HttpContext.Current.Session["username"] = "someone@blah.com";` – audiochick Feb 26 '14 at 11:06
  • Yes. i got null in sm_eid_val HttpContext.Current.Session[key] = value; HttpContext context = HttpContext.Current; string sm_eid_val = context.Request.Headers[key].ToString(); System.Diagnostics.Debug.WriteLine("SM_EID = " + sm_eid_val); – VimariE Feb 26 '14 at 11:13
  • My bad, sorry, I made a mistake with the previous comment, it should be `HttpContext.Request.Headers["username"] = "something@blah.com";` – audiochick Feb 26 '14 at 12:13
  • It is still not working. Haaay. Maybe there really is something wrong with what I am doing. – VimariE Feb 27 '14 at 02:38
  • Hi, i already passed the header, however when i load the other page, it seems that the header values are erased or not passed... I just want to ask if it is possible to redirect to another URL without losing the values in header. – VimariE Feb 28 '14 at 06:42
  • It doesn't look like you can do that easily, you can try doing it by setting the response status code to 307 or you can enter your headers as cookies, and then just read them from there. [RedirectHeaders](https://www.google.co.za/search?q=c%23+set+headers+for+redirect+page&oq=c%23+set+headers+for+redirect+page&aqs=chrome..69i57j69i58j0.9673j0j7&sourceid=chrome&espv=210&es_sm=93&ie=UTF-8) – audiochick Mar 03 '14 at 07:47