public void ConfigureServices(IServiceCollection services) { services.AddAuthentication(IISDefaults.AuthenticationScheme); //... }
public void Configure(IApplicationBuilder app) { app.UseAuthentication(); //... }
[Authorize] public class MyController : ControllerBase { [HttpGet] public IActionResult Get() { WindowsIdentity windowsIdentity = HttpContext.User.Identity as WindowsIdentity; if (windowsIdentity != null) { WindowsPrincipal windowsPrincipal = new WindowsPrincipal(windowsIdentity); string username = windowsIdentity.Name; //获取当前用户的用户名 bool isAuthorized = windowsPrincipal.IsInRole(WindowsBuiltInRole.Administrator); //.. }
//...
}
}