ASP.NET对IIS设置为匿名的Windows进行身份验证

问题描述 投票:0回答:1

是否可以拥有一个针对AD进行身份验证的ASP.NET站点并在设置为匿名的IIS站点中运行?

这是背景故事。我写了一个工作正常的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>

我尝试了几个不同的东西,但我总是为用户Anonymous获得401 Unauthorized。我想我只是对浏览器提示输入用户凭据并输入我的域\用户名时发生的事情感到困惑。

编辑看起来没有解决方法。当您将IIS设置为Anonymous时,浏览器不会将用户凭据发送到服务器,因此无法检查用户是谁。我将不得不打开Windows身份验证。

asp.net asp.net-mvc authentication iis asp.net-4.5
1个回答
0
投票

当你说“我的应用程序没有收到用户名”时,你究竟是什么意思?是否继续提示用户输入凭据?您也可以澄清您收到的401的子代码。

© www.soinside.com 2019 - 2024. All rights reserved.