【问题标题】:Django Authentication from .NET using HttpWebRequest and HttpWebResponse via HTTP POST使用 HttpWebRequest 和 HttpWebResponse 通过 HTTP POST 从 .NET 进行 Django 身份验证
【发布时间】:2010-09-27 06:50:12
【问题描述】:

我正在 .NET 中创建一个应用程序,它将作为我已经部署的 Django 应用程序的第二个 UI。对于某些操作,用户需要对自己进行身份验证(作为 Django 用户)。我使用了一种超级简单的方法来做到这一点(为简单起见,没有加密凭据):-

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

def authentication_api(request, raw_1, raw_2): 用户=验证(用户名=raw_1,密码=raw_2) 如果用户不是无: 如果 user.is_active: 返回 HttpResponse("正确", mimetype="text/plain") 别的: 返回 HttpResponse("disabled", mimetype="text/plain") 别的: 返回 HttpResponse("不正确", mimetype="text/plain")

第 2 步。我在 .NET 中使用以下代码调用它。下面的 'strAuthURL' 代表一个简单的 django URL 映射到上面的 django 视图:

昏暗请求 As HttpWebRequest = CType(WebRequest.Create(strAuthURL), HttpWebRequest) 昏暗响应 As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse) Dim reader As StreamReader = New StreamReader(response.GetResponseStream()) 暗淡结果 As String = reader.ReadToEnd() HttpWResp.Close()

这完美无缺,尽管它只是一个概念验证。

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

我为使用 POST 数据进行身份验证创建了一个 django 视图

def post_authentication_api(请求): 如果 request.method == 'POST': 用户 = 验证(用户名=request.POST['username'],密码=request.POST['password']) 如果用户不是无: 如果 user.is_active: 返回 HttpResponse("正确", mimetype="text/plain") 别的: 返回 HttpResponse("disabled", mimetype="text/plain") 别的: 返回 HttpResponse("不正确", 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:

昏暗请求 As HttpWebRequest = CType(WebRequest.Create(strAuthURL), HttpWebRequest) request.ContentType = "应用程序/x-www-form-urlencoded" request.Method = "POST" 暗淡编码作为新的 UnicodeEncoding Dim postData As String = "username=" & m_username & "&password=" & m_password 将 postBytes 调暗为 Byte() = encoding.GetBytes(postData) request.ContentLength = postBytes.Length 尝试 将 postStream 调暗为 Stream = request.GetRequestStream() postStream.Write(postBytes, 0, postBytes.Length) postStream.Close() 昏暗响应 As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse) 将 responseStream 调暗为新 StreamReader(response.GetResponseStream(), UnicodeEncoding.Unicode) 结果 = responseStream.ReadToEnd() 响应。关闭() 抓住前任作为例外 MessageBox.Show(ex.ToString) 结束尝试

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

谢谢, 厘米

【问题讨论】:

    标签: .net django authentication httpwebrequest httpwebresponse


    【解决方案1】:

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

    暗淡编码作为新的 UnicodeEncoding . 将 postBytes 调暗为 Byte() = encoding.GetBytes(postData)

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

    > 在我看来还可以。我建议使用 Wireshark 查看您的 restclient 在标头中发送的内容,并查看您的应用在标头中发送的内容。

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

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

    【讨论】:

    • 没错,那里有很棒的 HTTP 调试工具。我只是习惯了 Wireshark,从它作为 Ethereal 的时代开始。
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      在我看来还可以。我建议使用 Wireshark 查看您的 restclient 在标头中发送的内容,并查看您的应用在标头中发送的内容。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-13
        • 1970-01-01
        • 1970-01-01
        • 2012-04-20
        相关资源
        最近更新 更多