React.js + asp.net core + windows integrated single sign-on

Viewed 880

I have react app which will be run inside company network, where authorization has to be done against active directory server (security groups are basically application roles and they have to control permissions in the application). All users of the application has windows computers logged into domain.

My idea is, that for authorization to work, I need only two information inside react frontend

  1. windows username
  2. list of security groups related to application in which the user is in
    (security groups might be admin, user, reporter, controller etc. and user might be in multiple of these)

Now I am trying to create custom AuthorizationService, where the idea is that I will read windows username from httpContextAccessor and when I have the username I am free to query active directory for information about security groups. This technique will also allow me to fake username and role combinations for testing and debug purpose.

but i face the problem how to get that information from httpContextAccessor ?

My actual code

React App.js

Before main react component is mounted, I want to get username and list of roles from the API and store it in the session or storage so that each component can read it and handle authorization if needed. (with sync and async issues I will deal later)

componentWillMount() {
    this.authenticate();
}

authenticate() {
   axios.get('/api/auth/sso', { withCredentials: true })
      .then(response => {
          console.log(response);
          console.log(response.data);
       })
          .catch(error => {
               console.log(error);
           });
}

startup.cs

I added windows authentication to startup (I believe these settings should include user info into access context accessor but I am not sure)

public void ConfigureServices(IServiceCollection services)
{
     services.AddHttpContextAccessor(); // => added
     services.AddAuthentication(IISDefaults.AuthenticationScheme); // => added


public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
     if (env.IsDevelopment())
     {
          app.UseDeveloperExceptionPage();
     }
     else
     {
          app.UseExceptionHandler("/Error");
     }

     app.UseStaticFiles();
     app.UseSpaStaticFiles();
     app.UseAuthentication(); // => added

AuthController

public class AuthController : Controller
{
     private readonly IHttpContextAccessor _httpContextAccessor;

     public AuthController(IHttpContextAccessor httpContextAccessor)
     {
         _httpContextAccessor = httpContextAccessor;
     }

     [Route("api/auth/sso")]
     public IActionResult Index()
     {
         // Get ClaimsPrincipal username
         // var user = this.User.Identity.Name;                              // => return null
         var username = _httpContextAccessor.HttpContext.User.Identity.Name; // => return null

         var roles = new List<string>() { "admin", "user" };
         return Json(new { username, roles });
     }
 }

When I run the application, the /api/auth/sso is triggered, so I know my react code work's, but in the AuthController, I am not able to retrieve windows username from the _httpContextAccessor because Identity.Name is null and therefore I cannot start to query active directory :/

  • What am I missing here ?
  • What is good practice for this requirement ?
  • Is this good way to go or I have to look into some different approach, like IdentityServer and JWT ?
0 Answers
Related