Im trying to implerment some Credentials Security via. the auth headers.But the program hangs.. If i remove the securitypart everything works, without security thou. :)Any ideas?
It's basically causing the current request to request itself, which will continue until it eventually times out.
Have you considered making a seperate request to ensure the user is authenticated (possibly by passing in their credentials and checking if they are valid or not prior to performing any operations)? Or possibly using the [Authorize] attribute, which would only allow authenticated users to access a specific area :
[Authorize]
public String Get(int id)
{
//Your Code Here
}
which will cause an infinite loop to be created when you call response.GetResponse(). (Because you are attempting a request from your current location - which will create another request - which in turn creates another...)
Bormeth
Member
67 Points
85 Posts
HttpWebRequest and HttpWebResponse
Jan 30, 2013 05:21 PM|LINK
public ActionResult Index() { return View(); } public String Get(int id) { string url = HttpContext.Request.Url.AbsoluteUri; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "GET"; request.UseDefaultCredentials = false; request.PreAuthenticate = true; request.Credentials = new NetworkCredential("morten", "morten"); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); var rSet = dbEntities.ResturantSet.Where(x => x.Id == id).Single(); return JsonConvert.SerializeObject(rSet, Formatting.Indented, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore }); }Rion William...
All-Star
26516 Points
4405 Posts
Re: HttpWebRequest and HttpWebResponse
Jan 30, 2013 05:41 PM|LINK
The following line is causing an infinite loop :
It's basically causing the current request to request itself, which will continue until it eventually times out.
Have you considered making a seperate request to ensure the user is authenticated (possibly by passing in their credentials and checking if they are valid or not prior to performing any operations)? Or possibly using the [Authorize] attribute, which would only allow authenticated users to access a specific area :
[Authorize] public String Get(int id) { //Your Code Here }Bormeth
Member
67 Points
85 Posts
Re: HttpWebRequest and HttpWebResponse
Jan 30, 2013 06:37 PM|LINK
The "problem" is thats is an iphone trying to get some jSon... The username & password is a md5.
So i need the other thing to work.
any idea? :)
Bormeth
Member
67 Points
85 Posts
Re: HttpWebRequest and HttpWebResponse
Jan 30, 2013 07:29 PM|LINK
According to this i should be able to do what im doing?
http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.credentials.aspx
Rion William...
All-Star
26516 Points
4405 Posts
Re: HttpWebRequest and HttpWebResponse
Jan 30, 2013 07:47 PM|LINK
Right!
The example mentioned in the Microsoft article passes in a specific URL to create a new Web Request :
which when you call :
will perform the proper Request to that URL and recieve a Response.
However - in your example code you are passing in the current Request URL :
which will cause an infinite loop to be created when you call response.GetResponse(). (Because you are attempting a request from your current location - which will create another request - which in turn creates another...)
I hope this helped clarify things a bit.
Bormeth
Member
67 Points
85 Posts
Re: HttpWebRequest and HttpWebResponse
Jan 30, 2013 08:07 PM|LINK
Think im close to understand it now! :D
I can see the problem. Buut not quit sure how to solve it.
the method gets called via the Url www.site.dk/get/1 <-- forexample
the client calling i to have the auth header in place, or get a accesse denied.
Bormeth
Member
67 Points
85 Posts
Re: HttpWebRequest and HttpWebResponse
Feb 02, 2013 10:28 AM|LINK
Solved it :)
Changed it to search for HttpContext.Request.Headers["Authorization"]; and Covert it etc.
Rion William...
All-Star
26516 Points
4405 Posts
Re: HttpWebRequest and HttpWebResponse
Feb 02, 2013 01:15 PM|LINK
Glad that you got this taken care of.