Base solution for your next web application
Open Closed

Authentication payload in plain text format #4994


User avatar
0
Ricavir created

Hi,

I've an external web client that needs to connect to my aspnetcore server. The problem is that authentication request cames with a plain text format instead of json. The result is an HTTP 415 error.

Is it possible to configure authentication aspboilerplate server to accept plain text format as well ?


5 Answer(s)
  • User Avatar
    0
    alper created
    Support Team

    hi,

    Is External Authentication useful for you or did you check that ?

  • User Avatar
    0
    Ricavir created

    No, I don't think that external auth will help here.

    The problem is the format of the POST request sent to <a class="postlink" href="https://exampleurl.com/api/TokenAuth/Authenticate">https://exampleurl.com/api/TokenAuth/Authenticate</a>

    I've tested this with postman :

    • one request in JSON format > result : HTTP status = 200 OK
    • one request in TEXT (plain/text) format > result : HTTP status = 415 Unsupported Media Type

    See attached screenshots.

    The question is : can we have both formats JSON and Text (plain/text) supported for authentication ?

  • User Avatar
    0
    alper created
    Support Team

    Hi,

    Asp.Net Core does not know what to do with the text/plain content or how to map it and it fails! Normally it's impossible with the nature of AspNet Core dynamics. But I made a hackish way to accept text/plain request content-type.

    • Open TokenAuthController.cs in *.Web.Core project.

    • Add the method below to the TokenAuthController class

    [HttpPost]
    public async Task<AuthenticateResultModel> Authenticate2()
    {
    	string requestBody;
    	using (var reader = new StreamReader(Request.Body, Encoding.UTF8))
    	{
    		requestBody = await reader.ReadToEndAsync();
    	}
    
    	var authenticateModel = JsonConvert.DeserializeObject<AuthenticateModel>(requestBody);
    	return await Authenticate(authenticateModel);
    }
    
    • Send your request with text/plain content-type to Authenticate2 action.
    POST /api/TokenAuth/Authenticate2 HTTP/1.1
    Host: localhost:22742
    Content-Type: text/plain
    Cache-Control: no-cache
    
    {"userNameOrEmailAddress":"admin","password":"123456"}
    

    Voila! Successful result

    [attachment=0:262bh59y]text_plain_request_aspnet_core.jpg[/attachment:262bh59y]

  • User Avatar
    0
    Ricavir created

    Fantastic ! Thank you very much @Alper, you're great :)

  • User Avatar
    0
    alper created
    Support Team

    you're welcome