emonney / QuickApp

ASP.NET Core / Angular startup project template with complete login, user and role management. Plus other useful services for Quick Application Development
https://www.ebenmonney.com/quickapp
MIT License
1.26k stars 594 forks source link

Unable to login - The specified 'Content-Type' header is not valid. #80

Closed AnassL closed 6 years ago

AnassL commented 6 years ago

Unexpectedly and without any code change, the template stopped working. I enter the right user name and password but fail to login receiving the following error message:

Unable to login The specified 'Content-Type' header is not valid.

any idea ?

emonney commented 6 years ago

In order to login to the token endpoint you need the 'Content-Type': 'application/x-www-form-urlencoded'. Look in the file "endpoint-factory.service.ts" from the getLoginEndpoint method. What do you have there?

AnassL commented 6 years ago

here is the code snippet:

` getLoginEndpoint(userName: string, password: string): Observable {

    let header = new HttpHeaders({ 'Content-Type': 'application/x-www-form-urlencoded' });

    let params = new HttpParams()
        .append('username', userName)
        .append('password', password)
        .append('grant_type', 'password')
        .append('scope', 'openid email phone profile offline_access roles')
        .append('resource', window.location.origin);

    let requestBody = params.toString();

    return this.http.post<T>(this.loginUrl, requestBody, { headers: header });
}`

Compared to the original, it's identical.

And here is the console output:

alert.service.ts:150 Severity: "error", Summary: "Unable to login", Detail: "The specified 'Content-Type' header is not valid.", Error: "{"headers":{"normalizedNames":[],"lazyUpdate":null},"status":400,"statusText":"Bad Request","url":"http://localhost:65067/connect/token","ok":false,"name":"HttpErrorResponse","message":"Http failure response for http://localhost:65067/connect/token: 400 Bad Request","error":{"error":"invalid_request","error_description":"The specified 'Content-Type' header is not valid."}}"

Checked the last build, I still can login, which means that the database data is correct.

prvit commented 6 years ago

I faced the same issue. Before, it was working even sendint application/json Content-Type. Now, even sending application/x-www-form-urlencoded is not working.

image

The worst thing is I can't debug it, break point on the the first line of method Exchange is never hit. Response: {"error":"invalid_request","error_description":"The mandatory 'grant_type' parameter is missing."} But as you can see on the screenshot, grant_type is set to "password".

emonney commented 6 years ago

I believe you updated openiddict nuget package to a version with a breaking change. This library was in beta and had breaking changes with each update. Its currently in rc-final and fairly stable. Update your project to the latest version of openiddict (or to the one in this repository) and also update your project to match these two files AuthorizationController.cs and endpoint-factory.service.ts