代码之家  ›  专栏  ›  技术社区  ›  chefsmart

Django身份验证来自。NET通过HTTP POST使用HttpWebRequest和HttpWebResponse

  •  3
  • chefsmart  · 技术社区  · 17 年前

    我正在中创建一个应用程序。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。我在中使用以下代码调用了它。网。下面的“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来实现这一点,所以我做了以下工作:-

    我创建了一个django视图,用于使用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

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

    谢谢, 厘米

    3 回复  |  直到 17 年前
        1
  •  2
  •   Strelok    17 年前

    在我看来还可以。我建议使用Wireshark查看您的restclient在标头中发送了什么,以及您的应用程序在标头中传输了什么。

        2
  •  2
  •   Community Mohan Dere    9 年前

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

        3
  •  0
  •   chefsmart    17 年前

    它现在正在工作。HTTP标头正常,问题源于以下行:

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

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

    >在我看来还可以。我建议使用Wireshark查看您的restclient在标头中发送了什么,以及您的应用程序在标头中传输了什么。

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

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