【发布时间】:2017-09-06 21:49:45
【问题描述】:
我正在尝试在 .Net Core 2.0 空 Web 应用程序中混合使用 Windows 和 匿名 身份验证。我想避免使用 [Authorize] 属性,因为我不想使用 Mvc 或控制器。
我的设置如下:
我创建了一个空的 .Net Core 2.0 Web 应用程序
我转到项目属性 -> 调试 -> 选中“启用 Windows 身份验证”并禁用“启用匿名身份验证”。 现在 "windowsAuthentication": true 和 "anonymousAuthentication": false 出现在我的 "IIS" 下的 launchSettings.json 中。
在 Startup.cs 中,我在 ConfigureServices 中添加
services.AddAuthentication(Microsoft.AspNetCore.Server.IISIntegration.IISDefaults.AuthenticationScheme);如https://docs.microsoft.com/en-us/aspnet/core/migration/1x-to-2x/identity-2x#windows-authentication-httpsys--iisintegration 中所述我添加了一个简单的
Console.WriteLine(context.User.Identity.Name);以查看它在 app.Run 中的工作并... 一切正常!
但是...一旦我在 launchSettings.json 中将“anonymousAuthentication”设置为 true,它就会停止工作,我无法弄清楚我可以做些什么来使 Windows 身份验证与它一起工作。 Context.User.Identity.IsAuthenticated 总是错误的。
如您所见,我的配置非常简单,我需要它保持这种状态。我想在某些动态路由上启用/禁用 Windows 身份验证,因此不能选择使用具有 [Authorize] 属性的控制器。
我想要实现的是一个简单的应用程序,其中 url “/authenticated” 会回复 context.User.Identity.Name 的值,而 url “/public” 会回复类似“这是一个公共页面! ”。
类似于 NTLM authentication on specific route in ASP.NET Core 但没有 [Authorize] 属性和控制器的东西。
资源非常稀缺......有人知道我可能会错过什么吗?
谢谢!
【问题讨论】:
标签: c# asp.net .net authentication asp.net-core