Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Authentication 通过ngrok连接时未设置Cookies_Authentication_Django Rest Framework_Axios_Session Cookies_Ngrok - Fatal编程技术网

Authentication 通过ngrok连接时未设置Cookies

Authentication 通过ngrok连接时未设置Cookies,authentication,django-rest-framework,axios,session-cookies,ngrok,Authentication,Django Rest Framework,Axios,Session Cookies,Ngrok,我正在尝试对django应用程序使用会话身份验证。当我使用本地主机地址将前端应用程序连接到本地运行的后端服务器时,浏览器会设置Set Cookie头中发送的Cookie(包括会话id和csrf令牌),用户可以对自己进行身份验证。但是,当我使用指向同一服务器的ngrok URL时,浏览器不会设置会话id和csrf令牌cookies,并且用户被禁止访问受保护的端点 更多内容:我在前端使用axios。以及后端带有DRF的django。我正在尝试使用DRF的SessionAuthentication进行

我正在尝试对django应用程序使用会话身份验证。当我使用本地主机地址将前端应用程序连接到本地运行的后端服务器时,浏览器会设置Set Cookie头中发送的Cookie(包括会话id和csrf令牌),用户可以对自己进行身份验证。但是,当我使用指向同一服务器的ngrok URL时,浏览器不会设置会话id和csrf令牌cookies,并且用户被禁止访问受保护的端点

更多内容:我在前端使用axios。以及后端带有DRF的django。我正在尝试使用DRF的SessionAuthentication进行身份验证

Django设置文件

AUTHENTICATION_BACKENDS = [
    'django.contrib.auth.backends.ModelBackend',
]

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES' : [
        'rest_framework.authentication.SessionAuthentication',
        'knox.auth.TokenAuthentication',
    ]
}
class ProfileAPI(views.APIView):
    serializer_class = ProfileSerializer
    permission_classes = [IsAuthenticated & UserorAdminAccessOnly]

    def get(self, request):
        user = getCorrespondingUser(request)
        profile = Profile.objects.get(user=user)
        serializer = EmergencyFundSerializer(profile)
        self.check_object_permissions(self.request, profile)
        return Response(serializer.data)
验证端点

AUTHENTICATION_BACKENDS = [
    'django.contrib.auth.backends.ModelBackend',
]

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES' : [
        'rest_framework.authentication.SessionAuthentication',
        'knox.auth.TokenAuthentication',
    ]
}
class ProfileAPI(views.APIView):
    serializer_class = ProfileSerializer
    permission_classes = [IsAuthenticated & UserorAdminAccessOnly]

    def get(self, request):
        user = getCorrespondingUser(request)
        profile = Profile.objects.get(user=user)
        serializer = EmergencyFundSerializer(profile)
        self.check_object_permissions(self.request, profile)
        return Response(serializer.data)

修复方法是将以下内容添加到设置文件中,以更改django发送的默认SameSite cookie值:

SESSION_COOKIE_SAMESITE = None
CSRF_COOKIE_SAMESITE = None

对于无法识别这些设置的django版本,请安装django rest swagger并重试。

修复方法是将以下内容添加到设置文件中,以更改django发送的默认SameSite cookie值:

SESSION_COOKIE_SAMESITE = None
CSRF_COOKIE_SAMESITE = None
对于无法识别这些设置的django版本,请安装django rest swagger并重试