通过HTTP POST使用HttpWebRequest和HttpWebResponse从.NET进行Django身份验证

时间:2008-12-17 04:44:27

标签: .net django authentication httpwebrequest httpwebresponse

我在.NET中创建一个应用程序,它将作为我已部署的Django应用程序的第二个UI。对于某些操作,用户需要对自己进行身份验证(如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'表示映射到上面的django视图的简单django URL:     

    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数据进行身份验证创建了一个django视图     

    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

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

谢谢, CM

3 个答案:

答案 0 :(得分:2)

对我来说没问题。我建议您使用Wireshark查看您的restclient在标头中发送的内容,并查看您的应用在标头中发送的内容。

答案 1 :(得分:2)

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

答案 2 :(得分:0)

现在正在运作。 HTTP标头没问题,问题的根源如下:    

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

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

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

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

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