ASP.NET Core Identity with Windows Authentication

前端 未结 1 1443
刺人心
刺人心 2021-02-08 00:10

I\'m using .NET Core 3.0 Preview6.
We have an Intranet application with enabled Windows authentication which means that only valid AD users are allowed to use the applicatio

1条回答
  •  [愿得一人]
    2021-02-08 00:31

    This is fixed now. It's been a bug in the preview releases. Now it's working like intended. Good luck!

    Update: I'd like to post my working code for .NET Core 3.1 Final.

    1. It's essential to register the custom login middleware after framework middlewares in Configure:
        app.UseAuthentication();
        app.UseAuthorization();
        app.UseMiddleware();
    
    1. In the custom middleware, after signing in the user you must call CreateUserPrincipalAsync and save this principal to the HttpContext.User property.

      await signInManager.SignInAsync(user, true);
      context.User = await signInManager.CreateUserPrincipalAsync(user);
      
    2. For Blazor, we must use AuthenticationStateProvider. It has a property User which contains the ClaimsPrincipal from HttpContext. That's it.

    3. You are now able to get the Identity user like follows:

      var authState = await _authenticationStateProvider.GetAuthenticationStateAsync();
      var intranetUser = await UserManager.GetUserAsync(authState.User);
      

    0 讨论(0)
提交回复
热议问题