Windows authentication in asp.net 5

前端 未结 9 1431
别跟我提以往
别跟我提以往 2020-11-29 08:00

I am building an intranet application in ASP .NET 5, MVC 6. I want to know how to enable Windows Authentication.? The default project template supports only Individual User

9条回答
  •  伪装坚强ぢ
    2020-11-29 08:10

    In addition to the other answers here, which are for IIS-hosted only, you can enable Windows Authentication in a self-hosted ASP.NET 5 project (tested against beta 7 and beta 8) by adding the following in the Startup.cs Configure method, before the app.UseMvc or similar that you wish to protect:

    UPDATE FOR BETA 8

    // If we're self-hosting, enable integrated authentication (if we're using
    // IIS, this will be done at the IIS configuration level).
    var listener = app.ServerFeatures.Get();
    if (listener != null)
    {
        listener.AuthenticationManager.AuthenticationSchemes = 
            AuthenticationSchemes.NTLM;
    }
    

    PREVIOUS ANSWER FOR BETA 7

    // If we're self-hosting, enable windows/integrated authentication.
    // For IIS, this needs to be configured in IIS instead, and the
    // following will have no effect.
    if ((app.Server as ServerInformation) != null)
    {
      var serverInformation = (ServerInformation)app.Server;
      serverInformation.Listener.AuthenticationManager.AuthenticationSchemes = 
          AuthenticationSchemes.NTLM;
    }
    

    Adapted from the official MusicStore example.

    If you're debugging using Visual Studio 2015 with IIS Express, you can turn on Windows Authentication via a checkbox in the debug properties page for a project now, rather than messing with the applicationhost.config file. I couldn't get the web.config solution to work for IIS Express debugging, it throws an error about the configuration not being valid at that level. Note this does not currently work in beta 8 - see this issue

提交回复
热议问题