强制.NET 3.1中的窗口身份验证始终提示输入用户名和密码才能登录 [英] Force widows authentication in .NET 3.1 to always prompt for username and password to login

查看:82
本文介绍了强制.NET 3.1中的窗口身份验证始终提示输入用户名和密码才能登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用.NET3.1中的MVC核心架构运行Windows身份验证。我已经设置了一个运行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>

lanchsettings.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(&Q;域名组名称&Q;);

仅当您发送请求的工作站不是使用Windows身份验证的域的一部分时,Windows身份验证才会提示输入用户和密码。

这篇关于强制.NET 3.1中的窗口身份验证始终提示输入用户名和密码才能登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆