使用 Windows 身份验证的 ASP.NET Core 标识

Sve*_*ven 15 c# windows-authentication asp.net-core asp.net-core-identity

我正在使用 .NET Core 3.0 Preview6。
我们有一个启用了 Windows 身份验证的 Intranet 应用程序,这意味着只允许有效的 AD 用户使用该应用程序。
但是,我们喜欢使用 ASP.NET Identity 运行我们自己的身份验证后端,因为它“开箱即用”。我刚刚使用用户的 Windows 登录向 AspNetUsers 表添加了一列。

我想要完成的是 Windows 用户使用他们的 Windows 登录名自动登录到应用程序。
我已经创建了一个自定义身份验证中间件,请参阅下面的代码:

public class AutoLoginMiddleware
{
    private readonly RequestDelegate _next;
    private readonly ILogger _logger;

    public AutoLoginMiddleware(RequestDelegate next, ILogger<AutoLoginMiddleware> logger)
    {
        _next = next;
        _logger = logger;
    }

    public async Task InvokeAsync(HttpContext context, UserService userService, UserManager<IntranetUser> userManager, 
        SignInManager<IntranetUser> signInManager)
    {
        if (signInManager.IsSignedIn(context.User))
        {
            _logger.LogInformation("User already signed in");
        }
        else
        {
            if (context.User.Identity as WindowsIdentity != null)
            {
                _logger.LogInformation($"User with Windows Login {context.User.Identity.Name} needs to sign in");
                var windowsLogin = context.User.Identity.Name;


                var user = await userManager.Users.FirstOrDefaultAsync(u => u.NormalizedWindowsLogin == windowsLogin.ToUpperInvariant());

                if (user != null)
                {
                    await signInManager.SignInAsync(user, true, "automatic");
                    _logger.LogInformation($"User with id {user.Id}, name {user.UserName} successfully signed in");

                    // Workaround
                    context.Items["IntranetUser"] = user;
                }
                else
                {
                    _logger.LogInformation($"User cannot be found in identity store.");
                    throw new System.InvalidOperationException($"user not found.");
                }
            }
        }

        // Pass the request to the next middleware
        await _next(context);
    }
}
Run Code Online (Sandbox Code Playgroud)

文档说这SignInManager.SignInAsync会创建一个新的ClaimsIdentity- 但似乎永远不会发生 -HttpContext.User总是保持WindowsIdentity. 在用户再次登录的每个请求中,对 的调用signInManager.IsSignedIn()始终返回 false。

我现在的问题是:以这种方式进行自动身份验证通常是个好主意吗?还有哪些其他方式存在?

我的下一个要求是有一个自定义AuthorizationHandler. 这里的问题是有时在HandleRequirementAsync方法中AuthorizationHandlerContext.User.Identityis aWindowsIdentity然后调用context.User.Identity.Name引发以下异常:

System.ObjectDisposedException: Safe handle has been closed.

Object name: 'SafeHandle'.

   at System.Runtime.InteropServices.SafeHandle.DangerousAddRef(Boolean& success)

   at System.StubHelpers.StubHelpers.SafeHandleAddRef(SafeHandle pHandle, Boolean& success)

   at Interop.Advapi32.GetTokenInformation(SafeAccessTokenHandle TokenHandle, UInt32 TokenInformationClass, SafeLocalAllocHandle TokenInformation, UInt32 TokenInformationLength, UInt32& ReturnLength)

   at System.Security.Principal.WindowsIdentity.GetTokenInformation(SafeAccessTokenHandle tokenHandle, TokenInformationClass tokenInformationClass, Boolean nullOnInvalidParam)

   at System.Security.Principal.WindowsIdentity.get_User()

   at System.Security.Principal.WindowsIdentity.<GetName>b__51_0()

   at System.Security.Principal.WindowsIdentity.<>c__DisplayClass67_0.<RunImpersonatedInternal>b__0(Object <p0>)

   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
Run Code Online (Sandbox Code Playgroud)

我现在的假设是这两个部分不能很好地协同工作。有时似乎存在时间问题 - 我的自定义AuthorizationHandler在调用之间调用AutoLoginMiddleware

Sve*_*ven 12

现在已修复。这是预览版本中的一个错误。现在它按预期工作。祝你好运!

更新:我想发布我的 .NET Core 3.1 Final 工作代码。

  1. 在框架中间件之后注册自定义登录中间件是必不可少的Configure
    app.UseAuthentication();
    app.UseAuthorization();
    app.UseMiddleware<AutoLoginMiddleware>();
Run Code Online (Sandbox Code Playgroud)
  1. 在自定义中间件中,在用户登录后,您必须调用CreateUserPrincipalAsync此主体并将其保存到HttpContext.User属性中。

    await signInManager.SignInAsync(user, true);
    context.User = await signInManager.CreateUserPrincipalAsync(user);
    
    Run Code Online (Sandbox Code Playgroud)
  2. 对于 Blazor,我们必须使用AuthenticationStateProvider. 它有一个User包含ClaimsPrincipalfrom的属性HttpContext。就是这样。

  3. 您现在可以像下面这样获取 Identity 用户:

    var authState = await _authenticationStateProvider.GetAuthenticationStateAsync();
    var intranetUser = await UserManager.GetUserAsync(authState.User);
    
    Run Code Online (Sandbox Code Playgroud)