Guest User

Untitled

a guest
Apr 20th, 2015
229
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. private static string HandleRequest(HttpListenerContext context)
  2.         {
  3.             try
  4.             {
  5.                 // Check whether we got a successful response:
  6.                 string code = context.Request.QueryString["code"];
  7.                 if (!string.IsNullOrEmpty(code))
  8.                 {
  9.                     return code;
  10.                 }
  11.  
  12.                 // Check whether we got an error response:
  13.                 string error = context.Request.QueryString["error"];
  14.                 if (!string.IsNullOrEmpty(error))
  15.                 {
  16.                     return null; // Request cancelled by user.
  17.                 }
  18.  
  19.                 // The response is unknown to us. Choose a different authentication flow.
  20.                 throw new NotSupportedException(
  21.                     "Received an unknown response: " + Environment.NewLine + context.Request.RawUrl);
  22.             }
  23.             finally
  24.             {
  25.                 // Write a response.
  26.                 using (var writer = new StreamWriter(context.Response.OutputStream))
  27.                 {
  28.                     string response =
  29.                         "<head><title>{APP} - OAuth Authentication</title></head><body><h1>Authorization for {APP}</h1>" +
  30.                         "<p>The application has received your response. You can close this window now.</p>" +
  31.                         "<script type='text/javascript'>window.setTimeout(function() { window.open('', '_self', ''); window.close(); " +
  32.                         "}, 100); if (window.opener) { window.opener.checkToken(); } </script> </body> </html>";
  33.                     writer.WriteLine(response);
  34.                     writer.Flush();
  35.                 }
  36.                 context.Response.OutputStream.Close();
  37.                 context.Response.Close();
  38.             }
  39.         }
RAW Paste Data