IIS自动启动应用程序停止使用Windows身份验证

1aaf6o9v  于 2022-11-12  发布在  Windows
关注(0)|答案(1)|浏览(136)

我在IIS上有一个应用程序,我想自动启动。我按照这里的步骤操作,它工作了!https://www.taithienbo.com/how-to-auto-start-and-keep-an-asp-net-core-web-application-and-keep-it-running-on-iis/
但这是在IIS中的身份验证模式设置为匿名身份验证时。然后我想将Windows身份验证添加到我的应用程序中,我成功地做到了这一点。除了,我的网站的自动启动不再工作。您可以让您的IIS应用程序以Windows身份验证模式自动启动吗?如果可以,如何?

e4eetjau

e4eetjau1#

对于那些正在寻找答案的人-我也有同样的问题,我的解决方案是:
1.在web.config中全局启用匿名

<system.webServer>
  <security>
    <authentication>
        <windowsAuthentication enabled="true" />                
        <anonymousAuthentication enabled="true"/>
    </authentication>
  </security>
</system.webServer>

1.添加具有空Route和AllowAnonymous属性的默认控制器

[Route("")]
[ApiController]
[AllowAnonymous]
public class DefaultController : ControllerBase
{
    private readonly ILogger _logger;

    public DefaultController(ILogger logger)
    {
        _logger = logger;
    }

    [HttpGet]
    public ContentResult Get()
    {
        _logger.LogTrace("Warmup called");

        return Content("This is my webapi. see <a href='swagger/index.html'>here</a>",
        "text/html; charset=utf-8");
    }                
}

1.对于需要身份验证的控制器,请添加“授权”属性:

[Route("[controller]")]
[ApiController]
[EnableCors]
[Authorize]
public class MyAuthenticatedController : ControllerBase
{
    //...
}

您也可以添加一个单独的控制器来预热应用程序,并在applicationInitialization web.config的节中指定它的路由(适用于IIS〉= 8.0):

<system.webServer>
  <!-- ... -->
  <applicationInitialization doAppInitAfterRestart="true" skipManagedModules="false">
    <add initializationPage="/warmup" />
  </applicationInitialization>
</system.webServer>

最后,an answer对我很有帮助。

相关问题