2011-04-20 48 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都在那里!

任何有什么错误的想法?

回答

相关问题