Search code examples
oauth-2.0asp.net-core-webapiangular9aspnet-contribbattlenet-api

[Web API]AspNet.Security.OAuth.BattleNet OAuth 2.0 issue


Basically my Login with BattleNet Button sends request here:

     services.AddAuthentication(options =>
        {
            options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
        })
            .AddCookie(options =>
            {
                options.LoginPath = "/login";
                options.LogoutPath = "/logout";
            })
            .AddJwtBearer(options =>
            {
                options.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuer = true,
                    ValidateAudience = true,
                    ValidateLifetime = true,
                    ValidateIssuerSigningKey = true,
                    ValidIssuer = Configuration["Jwt:Issuer"],
                    ValidAudience = Configuration["Jwt:Audience"],
                    IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["Jwt:Key"]))
                };
            })
            .AddBattleNet(options =>
            {
                options.ClientId = Configuration["BattleNet:ClientId"];
                options.ClientSecret = Configuration["BattleNet:ClientSecret"];
                options.Region = BattleNetAuthenticationRegion.Europe;

            })

     [AllowAnonymous]
    [HttpPost(nameof(ExternalLogin))]
    public IActionResult ExternalLogin(string provider, string returnUrl = null)
    {
        var properties = new AuthenticationProperties
        {
            RedirectUri = Url.Action("ExternalLoginCallback"),
            Items =
            {
                { "scheme", provider},
                { "returnUrl", returnUrl }
            }
        };
        return Challenge(properties, provider);
    }

And suddenly I'm getting this error

CORS ERROR:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://eu.battle.net/oauth/authorize?client_id=*&scope=d3.profile%20openid&response_type=code&redirect_uri=https%3A%2F%2Flocalhost%3A44333%2Fapi%2Fuser%2Fexternallogincallback&state=CfDJ8GNva7rF4-dPly4Z3xuUAIb6108sCNggQc6reYXjMJrGmdWZbri-N715ueCZ2Ksw9Q47k8SX1xG-c3zDck-mXT8h8mvEY5_41ox7C9OsU-PStXqUsD6npNcMBhCr16Qh1valIQ6REKTMjLiilO5wbnx1349I2SwQfJevGwKZqC4o4PElfF6kPaPmRa5Eslquh4Sfwbn3HTKeaArxx2v8jD4. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).

CORS ERROR

HTTP REQUEST:

HTTP REQUEST

If I open this request in another tab I get authenticated and redirected to ExternalLoginCallback.

PS: I'm using AspNet.Security.OAuth.BattleNet and I'm not using ASP.NET Core Identity for Authentication (using custom logic).


Solution

  • You're not supposed to start an authorization code flow using AJAX/XHR. Instead, simply redirect your users to your ExternalLogin endpoint.