【问题标题】:Web Forms Windows Authentication w/ Remote SQL Database带有远程 SQL 数据库的 Web 窗体 Windows 身份验证
【发布时间】:2015-09-04 17:11:06
【问题描述】:

我有一个 ASP.NET 4.0 Web 应用程序,它使用针对 AD 的 Windows 身份验证和一个用于角色管理的 SQL Server。

基本上,我希望拥有 AD 帐户的所有用户都能够访问该应用程序,但我希望使用 Sql Server 中的角色进一步保护该应用程序。我不希望用户必须输入密码进行身份验证。

我是否可以在 Global Application_Start 方法中检查身份验证,还是应该在其他地方执行此代码?

【问题讨论】:

  • 这太宽泛了。根据您未提供给我们的信息,可能的答案太多了。

标签: c# asp.net webforms windows-authentication role-manager


【解决方案1】:

Application_Start 仅在应用程序本身初始化时触发一次。 HttpContext.Current.User 将包含用户发出导致 IIS 初始化应用程序的 HTTP 请求的详细信息。

改为使用Application_BeginRequest,它为每个传入请求引发,但理想情况下,您应该在 Web 应用程序打算执行操作时检查授权(而不是身份验证),而不是在每个请求上抢先。

【讨论】:

    【解决方案2】:

    经过进一步研究,我发现了“Application_AuthenticateRequest”,我认为它可以满足我使用 Windows 身份验证和 Sql Server 角色配置的目的。

        protected void Application_AuthenticateRequest(Object sender, EventArgs e)
        {
            if (Request.IsAuthenticated)
            {
                // just grab the username without domain info
                string[] arrTmp = HttpContext.Current.User.Identity.Name.Split('\\');
                string username = arrTmp[arrTmp.Length - 1];
    
                // Create an array of role names
                List<String> arrlstRoles = new List<String>();
    
                // work-around
                if (username == "fakename")
                    arrlstRoles.Add("Admin");
    
                // Add the roles to the User Principal
                HttpContext.Current.User = new System.Security.Principal.GenericPrincipal(User.Identity, arrlstRoles.ToArray<String>());
            }
        }
    

    【讨论】:

    • 不要使用ArrayList。使用List&lt;string&gt;ArrayList 已过时。
    • 谢谢约翰,我把那个换掉了。
    • 您也可以考虑在 AD 中保留角色。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-01
    • 1970-01-01
    • 2017-08-13
    • 1970-01-01
    • 1970-01-01
    • 2013-04-09
    • 1970-01-01
    相关资源
    最近更新 更多