在 .NET 3.1 中强制 widows 身份验证始终提示输入用户名和密码登录

Force widows authentication in .NET 3.1 to always prompt for username and password to login

我正在尝试 运行 windows 使用 .NET 3.1 中的 MVC 核心架构进行身份验证。我已经设置了一个 运行s windows 身份验证的项目,但每次我启动该应用程序时,它都会直接使用我在本地计算机上登录时使用的用户名登录。我需要它总是提示我,因为该应用程序将在公司范围内使用,我不想让任何用户登录它,另外我希望它能够让管理员从我的机器登录,这样并不总是我在登录。

告诉我如何在每次启动应用程序时手动强制提示登录。如果仅在单击按钮时才打开登录提示,那就更好了。

index.cshtml

@{
    ViewData["Title"] = "Home Page";
}

    <div class="text-center">
        <h1 class="display-4">You have loggin in as @User.Identity.Name</h1>
        <h1>@User.Identity.AuthenticationType</h1>
        <h1></h1>
        <h1>@User.Identity.Name</h1>
        <h1>@User.Identity.IsAuthenticated</h1>
    </div>

launchsettings.json

  "iisSettings": {
    "windowsAuthentication": true,
    "anonymousAuthentication": false,
    "iisExpress": {
      "applicationUrl": "http://localhost:65086",
      "sslPort": 44374
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "WindowsAuth": {
      "commandName": "Project",
      "launchBrowser": true,
      "applicationUrl": "https://localhost:5001;http://localhost:5000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

HomeController.cs

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using WindowsAuth.Models;

namespace WindowsAuth.Controllers
{
    
    public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;

        public HomeController(ILogger<HomeController> logger)
        {
            _logger = logger;
        }

        [Authorize]
        public IActionResult Index()
        {
            return View();
        }

        public IActionResult Privacy()
        {
            return View();
        }

        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }
    }
}

如果您认为我需要添加更多相关信息,请发表评论。

您不能使用 windows 身份验证来执行此操作。如果您需要登录提示,您应该使用表单身份验证。如果您不想让所有用户使用应用程序,您可以使用角色: User.IsInRole("域 Name\Group 名称");

Windows 仅当您发送请求的工作站不属于正在使用 windows 身份验证的域时,才提示输入用户名和密码。