验证是否在MVC5应用程序中正确设置了Active Directory Windows授权?

时间:2014-11-18 20:28:26

标签: c# asp.net asp.net-mvc authentication windows-authentication

我已经开始为内部网站开发一个小型网络应用程序。默认View将是一个简单的表,列出来自Oracle数据库的所有数据,其中包含几个用于搜索条件的文本框。此表中还有一个“管理”数据的视图,我想通过Active Directory凭据锁定。

参考ASP.Net文章“Autheticating Users with Windows Authentication”,我试图实现这一点。根据文章中的说明,我修改了 Windows 身份验证的主 Web.config

<authentication mode="Windows" />
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
    <httpModules>
      <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Extensibility.Web.RequestTracking.WebRequestTrackingModule, Microsoft.ApplicationInsights.Extensibility.Web" />
    </httpModules>

由于我目前正在通过Visual Studio附带的ASP.NET开发Web服务器进行开发,所以我没有看到此时需要启用NTLM身份验证或IIS功能(如果我忽略了某些内容,请随时纠正我主要的)。

然后我通过设置类似于示例的 Controllers \ HomeController.cs 继续关注本文:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;
using InventoryTracker.Models;
using InventoryTracker.DAL;
using System.Web.Mvc.Ajax;

namespace InventoryTracker.Controllers
{
    public class HomeController : Controller
    {
        InventoryTrackerContext _db = new InventoryTrackerContext();
        public ActionResult Index(INV_Assets defModel)
        {
            return View(defModel);
        }

        [Authorize(Roles="IT Group")]
        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }

        [Authorize(Roles="SomeDomain\\aguy")]
        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
    }
}

我发现奇怪的是,当我第一次登录时,我第一次被迫注册?注册后我现在可以登录,但我不确定[Authorize]属性是否实际生效。

例如,当我转到当前为About()设置的Roles="IT Group"页面时(之前使用的Roles="Managers"),而不是在那里,因为我是IT小组的成员,我被要求再次登录?

任何人都可以对我可能做错的事情提供一些见解吗?

1 个答案:

答案 0 :(得分:0)

运行Windows身份验证的应用程序的默认行为是自动登录。但是,如果您尝试访问您的用户未被授权访问的网站的某些部分,它可以回退到实际的登录表单,以便您有机会升级&#34;登录到您在系统上运行的帐户之外的其他帐户的权限。如果您因为您的用户帐户没有必要的权限而被投放到登录屏幕,则可以长短。

相关问题