【发布时间】: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:
服务器给我一个 500 内部服务器错误。我的猜测是 POST 请求未在 .NET 中正确设置。所以我基本上需要一些关于如何从 .NET 调用 django 视图发送 POST 数据的指导。
谢谢, 厘米
【问题讨论】:
标签: .net django authentication httpwebrequest httpwebresponse