2016-11-09 40 views
0

我試圖通過示例在IG文檔中實現服務器端身份驗證,但遇到了麻煩。 當我正在做我的請求,我已經得到狀態302發現的響應,並且在標題位置我沒有新的代碼重定向url,但是如果我在瀏覽器中做同樣的事情,一切都好。Instagram api服務器端身份驗證asp.net mvc

這裏是我的代碼和例子:

public JsonResult InstagramTeaser() 
    { 
     try 
     { 
      var clientId = Configuration.AuthKeys.InstagramOAuthClientId; 
      var clientSecret = Configuration.AuthKeys.InstagramOAuthClientSecret; 
      var redirectUrl = "http://localhost"; 
      var uri = $"http://api.instagram.com/oauth/authorize/?client_id={clientId}&redirect_uri={redirectUrl}&response_type=code"; 
      HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri); 
      request.AllowAutoRedirect = false; 
      string redirUrl = String.Empty; 
      using (HttpWebResponse response = (HttpWebResponse) request.GetResponse()) 
      { 
       int status = (int)response.StatusCode; // 302 Found 
       redirUrl = response.Headers[HttpResponseHeader.Location]; // And here I'm getting my old uri 
      } 

      return Json(redirUrl, JsonRequestBehavior.AllowGet); 
     } 
     catch (Exception e) 
     { 
      return Json(e.Message, JsonRequestBehavior.AllowGet); 
     } 
    } 

Results in browser

回答

0

的方式權威性在MVC的工作原理是,當你還沒有登錄,並嘗試 訪問它拋出一個安全頁面401例外。 MVC然後捕獲這個 異常,用戶在登錄頁面(也就是302 你看到的)

Here你可以得到你的問題的更多信息重定向