2014-02-19 104 views
1

我使用C#和Django实现了一个应用程序,我的问题是,当我在客户端登录时,服务器返回一切正确,sessionid和除csrf标记之外的所有东西。Django不返回CSRF令牌

我在中间件类的设置文件中有它。这是因为即时通过其IP地址直接访问服务器?

我的Django的登录功能

class loginMobile(GenericAPIView): 
    serializer_class = loginSerializer 

    def post(self, request): 
     try: 
      email = request.DATA.get('email') 
      password = request.DATA.get('password') 

      user_django = authenticate(username=email, password=password) 
      login(request, user_django) 

      return Response({'success': True}) 
     except: 
      return Response({'success': False}) 

我的C#的要求:

using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) 
       { 
        // Get the response stream 
        StreamReader reader = new StreamReader(response.GetResponseStream()); 

         if (response.Cookies != null && response.Cookies.Count != 0) 
         { 
          this.cookie_col = response.Cookies; 

          this.cookies = new List<Cookie>(); 
          foreach (Cookie c in response.Cookies) 
          { 
           this.cookies.Add(c); 
           if (c.Name.Equals("csrftoken")) 
            this.csrf_token = c.Value; 
          } 
         } 
       } 

在 “response.cookies” 我只得到了 “会话ID” 不是我 “csrftoken” 还发生这种情况托管在一个服务器的应用程序,它就像我的本地机器上的魅力

+0

会是这样的答案http://stackoverflow.com/a/16703297/202168我有我的C#应用​​程序的问题 – Anentropic

+0

通知,它在浏览器上工作正常...... – pedrotorres

+0

我对C#一无所知,但浏览器是否有可能从另一个页面查看csrf cookie,而您的C#应用​​程序只调用json(?)视图,而该视图缺少templatetag或装饰者告诉Django发送cookie?所以还http://stackoverflow.com/questions/20361653/template-less-django-ajax-does-djangos-csrf-token-get-updated-during-the-cou?rq=1 – Anentropic

回答

0

所以我弄清楚如何解决我的问题,

我强迫Django的返回CSRF令牌是这样的:

return Response({'success': True, 'csrf':csrf(request)})