首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不适用于Asp.Net的核心Windows身份验证

不适用于Asp.Net的核心Windows身份验证
EN

Stack Overflow用户
提问于 2018-05-17 20:57:49
回答 1查看 14.9K关注 0票数 10

当在IIS或Asp.Net中启用并运行windows身份验证时,IIS似乎无法从调用context?.User?.Identity?.Name中识别用户。

想要的行为:在IIS和/或IIS中启用Windows身份验证和匿名身份验证,Asp.Net核心应该自动识别windows用户。

实际行为:当我在IIS或IIS Express中同时启用窗口和匿名身份验证时,用户名为空。当我禁用匿名身份验证或调用HttpContext.ChallengeAsync(IISDefaults.AuthenticationScheme)时,我会得到一个登录提示,这是我不想要的。

我的理解是,即使我想将它用于Active Directory,我也不需要active目录或域来验证windows用户。

环境:

  • Windows 8.1 (不在域上)
  • IIS 8.5 / Visual Studio 2017 w/ IIS Express
  • 安装Windows身份验证安全功能
  • Windows身份验证&(使用NTLM提供程序)&启用匿名身份验证
  • 以本地帐户用户身份登录

依赖关系:

  • Microsoft.AspNetCore.All 2.0.8

启动:

代码语言:javascript
复制
public void ConfigureServices(IServiceCollection services)
{
    services.Configure<IISOptions>(iis =>
    {
        iis.AuthenticationDisplayName = "Windows";
        iis.AutomaticAuthentication = true;
    });

    services.AddAuthentication(IISDefaults.AuthenticationScheme);
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }

    app.UseAuthentication();
    app.Run(async (context) =>
    {
        await context.Response.WriteAsync(JsonConvert.SerializeObject(new
        {                
            UserName = context?.User?.Identity?.Name
        }));
    });       

launchSettings.json:

代码语言:javascript
复制
{
  "iisSettings": {
    "windowsAuthentication": true,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:51682/",
      "sslPort": 0      
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

web.config:

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <handlers>
      <remove name="aspNetCore" />
      <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
    </handlers>
    <aspNetCore forwardWindowsAuthToken="true" processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="true" stdoutLogFile=".\logs\stdout" />
  </system.webServer>
</configuration>

applicationhost.config:(IIS )

基于本文:https://learn.microsoft.com/en-us/iis/configuration/system.webServer/security/authentication/windowsAuthentication/

代码语言:javascript
复制
<authentication>
  <anonymousAuthentication enabled="true" userName="" />
  <basicAuthentication enabled="false" />
  <clientCertificateMappingAuthentication enabled="false" />
  <digestAuthentication enabled="false" />
  <iisClientCertificateMappingAuthentication enabled="false"></iisClientCertificateMappingAuthentication>
  <windowsAuthentication enabled="true">
    <providers>
      <add value="NTLM" />
    </providers>
  </windowsAuthentication>
</authentication>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-05-18 12:53:06

几件事:

  1. 禁用匿名身份验证时,会弹出一个弹出,因为浏览器可能不信任该站点。您需要打开互联网选项(从) ->安全选项卡->点击‘可信站点’->点击网站,并添加到您的网站那里。IE和Chrome都使用这些设置来决定是否自动发送凭据。
  2. 启用匿名身份验证和Windows身份验证时,匿名优先,除非您告诉它必须登录用户。要做到这一点,可以在控制器上或仅对单个操作使用[Authorize]属性。在允许匿名访问标题下,文档中有更多的细节。
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50400393

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档