Showing posts with label React. Show all posts
Showing posts with label React. Show all posts

Sunday, February 19, 2023

How to connect ASP.NET Core 6 API with React JS using Windows Authentication

 Recently I had to setup ReactJs application to support Windows Authentication with ASP.NET Core 6 API. Authorization will be handled via LDAP which is not included in this post.

I'm using axios as the http client to query API. Let's start creating a simple ReactJs Application.

Step 01 - Create React SPA.

npx create-react-app react-app-with-windows-auth
cd react-app-with-windows-auth
npm start

Refer Getting Started Guide for more information.

Step 02 - Setup axios

Axios is a promise-based HTTP Client for node.js and the browser. It is isomorphic (= it can run in the browser and nodejs with the same codebase). On the server-side it uses the native node.js http module, while on the client (browser) it uses XMLHttpRequests. Refer Getting Started for more information

npm install axios

Step 03 - Create ASP.NET Core 6 API Project

dotnet new webapi -o TodoApi
cd TodoApi

Step 04 - Add windows authentication to 6 API Project

in launchSettings.json file add following settings.

"iisSettings": {
    "windowsAuthentication": true,
    "anonymousAuthentication": false,
  }

Step 05 - Add CORS for Web Api Project

in Program.cs file add following settings.

using Microsoft.AspNetCore.Authentication.Negotiate;

var builder = WebApplication.CreateBuilder(new WebApplicationOptions
{
    Args = args,
    ContentRootPath = Directory.GetCurrentDirectory()
});

builder.Services.AddHttpContextAccessor();
builder.Services.AddCors(options =>
{
    options.AddDefaultPolicy(
        policy =>
        {
            policy
                    .WithOrigins("http://localhost:44426") // ReactApp Url
                    .AllowAnyHeader()
                    .AllowAnyMethod()
                    .AllowCredentials();
        });
});

// Add services to the container.

builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

builder.Services.AddControllersWithViews();

builder.Services.AddAuthentication(NegotiateDefaults.AuthenticationScheme)
   .AddNegotiate();

builder.Services.AddAuthorization(options =>
{
    options.FallbackPolicy = options.DefaultPolicy;
});

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment() || app.Environment.IsProduction())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseStaticFiles();
app.UseRouting();

app.UseCors();

app.UseAuthentication();
app.UseAuthorization();

app.MapControllerRoute(
    name: "default",
    pattern: "{controller}/{action=Index}/{id?}").RequireAuthorization();

app.MapFallbackToFile("index.html");

app.Run();

Step 06 - Add Axios settings

 async populateWeatherData() {

        try {
            const response = await axios.get('http://localhost:5000/weatherforecast/GetWeatherForecast', { withCredentials: true });
            console.log(response);
            this.setState({ forecasts: response.data, loading: false });
        } catch (error) {
            console.error(error);
        }
    }

Step 07 - Run both React App and Backend API

It will prompt small window to enter windows login and password when you try to access api endpoints. You can observe the same popup when you run swagger endpoint too

Step 08 - Working solution can be found in my git account

Tuesday, June 4, 2019

Android project not found. Maybe run react-native android first?

You may encounter this "Android project not found. Maybe run react-native android first" error message when you try to deploy the react-native application to your mobile device. As far as I figured there are two things that you may have to do while doing a fix for this issue.

1. Straight forward solution.  
In most of the cases, this will sort out this error by upgrading the react-native package in the root folder.
  • Go to the Project Root.
  • Upgrade the React native package in the Command Prompt by typing :
        react-native upgrade
  • Then Accept to update all the Files by typing y (Yes) in the Command Prompt.
2. GIT based upgrade.
You might see another option saying to upgrade git related package as the following message:
"You should consider using the new upgrade tool based on Git. It makes upgrades easier by resolving most conflicts automatically."

  • Go back to the old version of React Native.
  • Run npm install -g react-native-git-upgrade
  • Run react-native-git-upgrade
See https://facebook.github.io/react-native/docs/upgrading.html
Your project needs to have a name, declared in package.json, such as "name": "AwesomeApp". Please add a project name. Aborting.