【发布时间】:2018-07-19 16:55:07
【问题描述】:
是否可以有一个针对 AD 进行身份验证并在设置为匿名的 IIS 站点中运行的 ASP.NET 站点?
这是背景故事。我编写了一个运行良好的 ASP.NET 站点,但它正在部署到没有 Windows 身份验证设置的服务器上,我猜想打开它有些担心,因为该服务器上还有其他站点。我的网站有一个自定义 AuthorizeAttribute 来检查登录用户是否与这样的用户或组列表匹配:
[AuthorizeUsers(Groups = @"Admins", Users = @"domain\user1,domain\user2")]
public class HomeController : Controller
现在我的想法是,如果 IIS 设置为匿名就很好,因为一旦用户点击我的应用程序,这个AuthorizeAttribute 就会启动,他们会收到提示输入凭据,这些会被传入,并且会得到验证。虽然这不会发生。即使在收到提示后,我的应用程序也没有收到他们的用户名。这是其余的相关代码。
public class AuthorizeUsers : AuthorizeAttribute
{
public string Groups { get; set; }
public string Users { get; set; }
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
//This check ends up always being false
//I've tried commenting it out, but httpContext.User.Identity.Name always comes in empty
if (base.AuthorizeCore(httpContext))
{
if (!String.IsNullOrEmpty(Groups))
{
try
{
// Get the AD groups
var groups = Groups.Split(',').ToList<string>();
// Verify that the user is in the given AD group (if any)
var context = new PrincipalContext(ContextType.Domain);
var userPrincipal = UserPrincipal.FindByIdentity(context,
IdentityType.SamAccountName,
httpContext.User.Identity.Name);
foreach (var group in groups)
{
if (userPrincipal.IsMemberOf(context, IdentityType.Name, group))
{
return true;
}
}
} catch (Exception e)
{
//string msg = e.Message;
}
}
if (!String.IsNullOrEmpty(Users))
{
try
{
// Get the AD groups
var users = Users.Split(',').ToList<string>();
string username = httpContext.User.Identity.Name;
foreach (var name in users)
{
if (name.Equals(username, StringComparison.InvariantCultureIgnoreCase))
{
return true;
}
}
} catch (Exception e)
{
//string msg = e.Message;
}
}
}
return false;
}
}
这里是 web.config
<authentication mode="None" />
<authorization>
<allow users="*" />
</authorization>
<roleManager enabled="true" defaultProvider="AspNetWindowsTokenRoleProvider">
<providers>
<clear />
<add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/" />
</providers>
</roleManager>
我尝试了几种不同的方法,但我总是收到用户匿名的 401 Unauthorized。我想我只是对浏览器提示输入用户凭据并输入我的域\用户名时发生的事情感到困惑。
编辑 看起来没有解决方法。当您将 IIS 设置为匿名时,浏览器不会将用户凭据发送到服务器,因此无法检查用户是谁。我将不得不打开 Windows 身份验证。
【问题讨论】:
标签: asp.net asp.net-mvc authentication iis asp.net-4.5