Myosotis
Myosotis

Reputation: 33

Connection error when sending web requests on unity WebGL build only

I'm trying to send a post request to my login API (located on same system) that I made on ASP.NET in c# with my unity WebGL build as client, but for some reason it always pops out a connection error on client-side immediately while nothing is popping up on the console for my API. enter image description here Here is my code for the post request:

using(UnityWebRequest apiRequest = UnityWebRequest.Post(uri, jsonRequest, "application/json")){
    apiRequest.SetRequestHeader("Access-Control-Allow-Origin", "*");
    apiRequest.SetRequestHeader("Access-Control-Allow-Methods", "*");
    apiRequest.SetRequestHeader("Access-Control-Allow-Headers", "*");
    yield return apiRequest.SendWebRequest();

    //apiRequest returns a token on successful authentication
    if (apiRequest.result > UnityWebRequest.Result.Success){
        Debug.Log("API Request: " + apiRequest.result);
        loginPending = false;
        yield break;
    }
    else if (apiRequest.downloadHandler.text.ToLower() == "invalid credentials"){
        Debug.Log("Login failed");
        loginPending = false;
        yield break; 
    }
    else token = apiRequest.downloadHandler.text;
}

I want to also add that this worked on the editor and on the desktop build.

One thing I've tried was enabling CORS on both the WebGL client and API, I'm not too sure if I properly setup the one on the API but this is the one I have and it still did not work and I got the same error:

var builder = WebApplication.CreateBuilder(args);
var settings = new Settings();

builder.Services.AddCors(options =>
{
    options.AddPolicy(name: "WebGLOrigins",
        policy =>
        {
            policy.AllowAnyOrigin();
            policy.AllowAnyMethod();
            policy.AllowAnyHeader();
        });
});

builder.Configuration.Bind("Settings", settings);
builder.Services.AddSingleton(settings);

// Add services to the container.

builder.Services.AddControllers();

builder.Services.AddEndpointsApiExplorer();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    
}
app.UseHttpsRedirection();
app.UseCors("WebGLOrigins");

app.UseAuthorization();

app.MapControllers();

app.Run();

My controller containing the post request:

[EnableCors("WebGLOrigins")]
[HttpPost("login")]
public string Post([Required] LoginCreds loginCreds){
    loginCreds.playerName = loginCreds.playerName.ToLower();

    //MAKE SURE TO SET USER OFFLINE WHEN LOGGED OUT OR DISCONNECTED
    SqlConnection sqlConn = _playerService.SqlOpen();
    if (_playerService.IsUserOnline(sqlConn, loginCreds.playerName)) return "User already Online";

    //sanitize user input before authenticating login creds
    (bool, string) authenticateResult = _playerService.LdapAuthenticate(loginCreds.playerName, loginCreds.playerPass);
    if(authenticateResult.Item1) _playerService.SendTokenToDB(loginCreds.playerName, authenticateResult.Item2);

    Console.WriteLine("LDAP API service finished");
    return authenticateResult.Item2;
}

Upvotes: 0

Views: 416

Answers (1)

Myosotis
Myosotis

Reputation: 33

So the way to solve this was to also add the [EnableCors("WebGLOrigins")] attribute on top of the controller class that held the post method I've been trying to use. I only saw that you can do that on this article

https://dzone.com/articles/cors-in-net-core-net-core-security-part-vi

Upvotes: 0

Related Questions