2011-04-20 77 views
0

我試圖創建一個ASP.Net MVC端點來進行外部身份驗證。這個想法是這樣的,我可以從控制檯應用程序,WPF應用程序或其他任何應用程序中調用端點,併爲我的服務使用MVC模式,將JSON返回給經過身份驗證的用戶,通過屬性等檢查身份驗證。我使用控制檯應用程序現在只是因爲它快速簡單。控制檯應用程序中的ASP.Net MVC Cookie

我有這個至今:

在我的控制檯應用程序:

Public Sub MakeLoginRequest() 
     Dim address As Uri = New Uri("http://localhost:50536/Account/LogIn") 
     Dim request As HttpWebRequest = HttpWebRequest.Create(address) 
     request.Method = "POST" 
     request.ContentType = "application/json; charset=utf-8" 

     Dim loginModel As New LogOnModel With {.UserName = "Richard", 
               .Password = "Password1", 
               .RememberMe = False} 

     Dim jsonData As String = JsonConvert.SerializeObject(loginModel) 
     Dim bytes As Byte() = System.Text.Encoding.ASCII.GetBytes(jsonData) 
     request.GetRequestStream.Write(bytes, 0, bytes.Count) 

     Dim response As HttpWebResponse = request.GetResponse() 
End Sub 

在我的控制器:

<HttpPost()> 
Public Function LogIn(model As LogOnModel) As ActionResult 
    If ModelState.IsValid Then 
     If Membership.ValidateUser(model.UserName, model.Password) Then 
      Dim cookie As HttpCookie = FormsAuthentication.GetAuthCookie(model.UserName, False) 
      cookie.Expires = DateTime.Now.AddMinutes(20) 
      Request.Cookies.Add(cookie) 
      Request.Cookies.Add(New HttpCookie("Barney", "Rubble")) 

      Return Content("Logged In Ok") 
     Else 
      Return New HttpUnauthorizedResult 
     End If 
    Else 
     Return New HttpUnauthorizedResult 
    End If 
End Function 

現在,當我檢查的控制檯應用程序的響應,有從來沒有任何餅乾 - 既沒有真正的Auth cookie,也沒有我的假Barney Rubble餅乾實際上出現!

但是...我在Chrome中進行相同的調用並檢查響應...並且這兩個cookie都在那裏!

任何有什麼錯誤的想法?

回答

相關問題