0

I am trying to implement the single sign-on using Angular, Django, IIS server.

In IIS windows authentication is enabled.

Angular intercepter code :

intercept(req: HttpRequest<any>, next: HttpHandler):Observable<HttpEvent<any>> {   
 console.log("in intercept")   
 req = req.clone({  
 withCredentials: true });    
return next.handle(req);  }

Django settings.py:

MIDDLEWARE = [    
'django.middleware.security.SecurityMiddleware',    'django.contrib.sessions.middleware.SessionMiddleware',    'corsheaders.middleware.CorsMiddleware',    'django.middleware.common.CommonMiddleware',    'django.middleware.csrf.CsrfViewMiddleware',    'django.contrib.auth.middleware.AuthenticationMiddleware',    'django.contrib.auth.middleware.RemoteUserMiddleware',    'django.contrib.messages.middleware.MessageMiddleware',    'django.middleware.clickjacking.XFrameOptionsMiddleware',]

AUTHENTICATION_BACKENDS = (    'django.contrib.auth.backends.RemoteUserBackend',)

CORS_ORIGIN_ALLOW_ALL = True

ALLOWED_HOSTS = ["*"]

Getting error: (IP-address) has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Sidhartha
  • 988
  • 4
  • 19
  • 39

3 Answers3

1

The problem will lie in the Django setup, please have a look at this link: https://stackoverflow.com/a/38162454/4587598

If at first try won't work, strip all settings.py and setup from scratch, firstly checking if CORS issue does not occur and afterwards add authentication complexity.

Mac_W
  • 2,927
  • 6
  • 17
  • 30
1

Try this configuration in settings.py

CORS_ORIGIN_ALLOW_ALL = True CORS_ALLOW_CREDENTIALS = True

CORS_ALLOW_CREDENTIALS = True # This one is required when you are using withCredentials: true

partha
  • 154
  • 7
0

try django-cors-headers pip install django-cors-headers

And set it up In your settings.py

INSTALLED_APPS = (
    ...
    'corsheaders',
    ...
)

You will also need to add a middleware class to listen in on responses:

MIDDLEWARE = [  # Or MIDDLEWARE_CLASSES on Django < 1.10
    ...
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.common.CommonMiddleware',
    ...
]

CorsMiddleware should be placed as high as possible, especially before any middleware that can generate responses such as Django's CommonMiddleware

CORS_ORIGIN_ALLOW_ALL = True
Zeyad Obaia
  • 686
  • 1
  • 6
  • 21