2008-12-17 76 views
3

我在.NET中创建了一个应用程序,该应用程序将作为我已部署的Django应用程序的第二个UI。对于用户需要进行身份验证的一些操作(如Django用户)。我使用了一种非常简单的方式来完成此操作(无简单加密凭证): -从.NET使用HttpWebRequest和HttpWebResponse通过HTTP POST进行Django身份验证

第1步。我创建了一个django视图,它通过两个HTTP GET参数接受用户名和密码,并将它们传递给django.contrib .auth.authenticate()作为关键字参数。请参阅下面的代码:

 
    def authentication_api(request, raw_1, raw_2): 
     user = authenticate(username=raw_1, password=raw_2) 
     if user is not None: 
      if user.is_active: 
       return HttpResponse("correct", mimetype="text/plain") 
      else: 
       return HttpResponse("disabled", mimetype="text/plain") 
     else: 
      return HttpResponse("incorrect", mimetype="text/plain")

第2步。我在.NET中使用以下代码调用此代码。在下面的“strAuthURL” representes映射到上述Django的视图简单的Django网址:

 
    Dim request As HttpWebRequest = CType(WebRequest.Create(strAuthURL), HttpWebRequest) 
    Dim response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse) 
    Dim reader As StreamReader = New StreamReader(response.GetResponseStream()) 
    Dim result As String = reader.ReadToEnd() 
    HttpWResp.Close()

这完美的作品,但它只不过是一个证明的概念更多。

现在我想,所以我做了以下通过HTTP POST做到这一点: -

我使用POST数据

 
    def post_authentication_api(request): 
     if request.method == 'POST': 
      user = authenticate(username=request.POST['username'], password=request.POST['password']) 
      if user is not None: 
       if user.is_active: 
        return HttpResponse("correct", mimetype="text/plain") 
       else: 
        return HttpResponse("disabled", mimetype="text/plain") 
      else: 
       return HttpResponse("incorrect", mimetype="text/plain")

I have tested this using restclient and this view works as expected. However I can't get it to work from the .NET code below:

 
    Dim request As HttpWebRequest = CType(WebRequest.Create(strAuthURL), HttpWebRequest) 
    request.ContentType = "application/x-www-form-urlencoded" 
    request.Method = "POST" 
    Dim encoding As New UnicodeEncoding 
    Dim postData As String = "username=" & m_username & "&password=" & m_password 
    Dim postBytes As Byte() = encoding.GetBytes(postData) 
    request.ContentLength = postBytes.Length 
    Try 
     Dim postStream As Stream = request.GetRequestStream() 
     postStream.Write(postBytes, 0, postBytes.Length) 
     postStream.Close() 
     Dim response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse) 
     Dim responseStream As New StreamReader(response.GetResponseStream(), UnicodeEncoding.Unicode) 
     result = responseStream.ReadToEnd() 
     response.Close() 
    Catch ex As Exception 
     MessageBox.Show(ex.ToString) 
    End Try

创造了做认证Django视图服务器给我一个500内部服务器错误。我的猜测是POST请求没有在.NET中正确设置。所以我基本上需要一些关于如何从.NET发送POST数据来调用django视图的指导。

感谢, CM

回答

2

看起来不错给我。我建议使用Wireshark来查看您的restclient在标题中发送的内容,并查看您的应用程序在标题中发送的内容。

2

如果您没有在两个站点之间进行任何会话共享,并且.Net站点可以访问Django应用程序的数据库,则可以直接对数据库进行身份验证。 This问题是关于如何从Python到.Net,但它应该帮助你,当你的哈希不完全匹配。

0

它现在正在工作。 HTTP头是好的,问题的根源在于以下几行:

 
    Dim encoding As New UnicodeEncoding 
    . 
    Dim postBytes As Byte() = encoding.GetBytes(postData)

本质上,这导致数据流中字符字节之间为空字节。这当然不是Django认证期望的参数。改用ASCIIEncoding解决了这个问题。

>看起来没问题。我建议使用Wireshark来查看您的restclient在标题中发送的内容,并查看您的应用程序在标题中发送的内容。

这让我走上了解决这个问题的正确道路。我尝试了Wireshark,但后来使用了HTTP调试器,它似乎更适合这项工作。

正确的工具可以节省一个小时!

+0

没错,那里有很棒的HTTP调试工具。从Ethereal开始,我就习惯了Wireshark。 – Strelok 2008-12-17 23:31:33

相关问题