在_Layout.chstml / Windows中,@ User.Identity.Name为空它在IIS中托管时,Auth似乎不起作用

时间:2018-04-11 22:32:09

标签: c# iis asp.net-core windows-authentication razor-pages

使用非常基本的dotnetcore2 ef razor页面应用程序,当我在我的开发机器上时,_Layout.cshmtl中的@ User.Identity.Name可以正常工作。当我使用IIS将代码放在我的服务器上时,尽管我做了最好的尝试,@ User.Identity.Name仍然拒绝为空。

我花了好几个小时在网上搜索并尝试了我能找到的所有内容并且无法克服这个问题。

这些似乎适合所有人的事情......

  • 确保IIS启用了Windows身份验证并禁用了禁用身份验证
  • 确保应用程序属性启用了Windows身份验证并禁用了身份验证 停用
  • 确保forwardWindowsAuthToken =" true"属性在web.config中设置
  • 注册IIS身份验证服务:services.AddAuthentication(IISDefaults.AuthenticationScheme);到startup.cs

...但是当使用IIS在服务器上启动时,@ User.Identity.Name在_Layout.chsmtl中对我来说总是空的。

以下是我的web.config文件的内容:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <handlers>
      <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
    </handlers>
    <aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="true" stdoutLogFile="f:\wwwroot\App\stdout.log" forwardWindowsAuthToken="true" />
  </system.webServer>
</configuration>

...我的Layout.cshtml文件的内容:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>@ViewData["Title"] - Title goes here</title>

    <environment include="Development">
        <link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.css" />
        <link rel="stylesheet" href="~/css/site.css" />
    </environment>
    <environment exclude="Development">
        <link rel="stylesheet" href="https://ajax.aspnetcdn.com/ajax/bootstrap/3.3.7/css/bootstrap.min.css"
              asp-fallback-href="~/lib/bootstrap/dist/css/bootstrap.min.css"
              asp-fallback-test-class="sr-only" asp-fallback-test-property="position" asp-fallback-test-value="absolute" />
        <link rel="stylesheet" href="~/css/site.min.css" asp-append-version="true" />
        <link rel="stylesheet" href="~/css/site.css" />
    </environment>
</head>
<body>
     <nav class="navbar navbar-inverse navbar-fixed-top navbar">
        <div class="container">
           <div class="row">
            <div class="navbar-header col-md-1">
                <div class="dropdown">
                    <button type="button" class="settingsbutton" data-toggle="dropdown">
                        <span class="glyphicon glyphicon-menu-hamburger"></span>
                    </button>
                    <ul class="dropdown-menu">
                        <li><a href="#"><span class="glyphicon glyphicon-user"></span> Change User Settings</a></li>
                        <li><a href="#"><span class="glyphicon glyphicon-globe"></span> Contact Admin</a></li>
                        <li><a asp-page="Create"><span class="glyphicon glyphicon-cog"></span> Create New Records</a></li>
                        <li><a asp-controller="Request" asp-action="Index"><span class="glyphicon glyphicon-arrow-left"></span> Return to Queue</a></li>
                    </ul>
                </div>
            </div>
            <div class="col-md-6">
                  <p class="headertext">Header</p>
            </div>

            <div class="col-md-5">
                <p class="nav adsusertext navbar-right"><span class="glyphicon glyphicon-user"></span> @User.Identity.Name</p>
            </div>
  </div>
        </div>
    </nav>
    <div class="container body-content">
        @RenderBody()
        <hr />
        <footer>
            <p>footer</p>
        </footer>
    </div>

    <environment include="Development">
        <script src="~/lib/jquery/dist/jquery.js"></script>
        <script src="~/lib/bootstrap/dist/js/bootstrap.js"></script>
        <script src="~/js/site.js" asp-append-version="true"></script>
    </environment>
    <environment exclude="Development">
        <script src="https://ajax.aspnetcdn.com/ajax/jquery/jquery-2.2.0.min.js"
                asp-fallback-src="~/lib/jquery/dist/jquery.min.js"
                asp-fallback-test="window.jQuery"
                crossorigin="anonymous"
                integrity="sha384-K+ctZQ+LL8q6tP7I94W+qzQsfRV2a+AfHIi9k8z8l9ggpc8X+Ytst4yBo/hH+8Fk">
        </script>
        <script src="https://ajax.aspnetcdn.com/ajax/bootstrap/3.3.7/bootstrap.min.js"
                asp-fallback-src="~/lib/bootstrap/dist/js/bootstrap.min.js"
                asp-fallback-test="window.jQuery && window.jQuery.fn && window.jQuery.fn.modal"
                crossorigin="anonymous"
                integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa">
        </script>
        <script src="~/js/site.min.js" asp-append-version="true"></script>
    </environment>
    @RenderSection("Scripts", required: false)
</body>
</html>

...我的Startup.cs文件的内容:

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Server.IISIntegration;
using Foo.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Identity;
using Foo.Pages.Request;

namespace Foo
{
    public class Startup
    {

        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;

        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {

            //Register MVC using /Pages/Request razor page as the default start folder
            services.AddMvc()
              .AddRazorPagesOptions(options =>
              {
                  options.RootDirectory = "/Pages/Request";
              });;

            //Register the dbcontext
            services.AddDbContext<dbContext>(options =>
            options.UseSqlServer(Configuration.GetConnectionString("db"), b => b.UseRowNumberForPaging()));

            //Register IIS authentication services
            services.AddAuthentication(IISDefaults.AuthenticationScheme);

        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {


            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Error");
            }

            app.UseStaticFiles();

            app.UseMvc();

            app.UseAuthentication();

            }
    }
}

...以及我的Program.cs的内容,如果重要的话:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.Server.HttpSys;
using Microsoft.Extensions.DependencyInjection;
using Foo.Models;
using Novell.Directory.Ldap;


namespace Foo
{

    public class Program
    {

        public static void Main(string[] args)
        {
            var host = BuildWebHost(args);
              host.Run();
        }

        public static IWebHost BuildWebHost(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseKestrel()
                .UseIISIntegration() 
                .UseStartup<Startup>()
                .Build();
    }

}

这让我疯了,如果有人有任何想法,他们会非常感激!!!!

0 个答案:

没有答案
相关问题