c# - 可以根据 Windows 用户名自动登录 Forms 身份验证的 MVC 应用程序吗?

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

我有一个使用表单例份验证的 asp.net mvc 应用程序,即用户/密码在数据库中。这个应用程序被几个客户在他们自己的服务器上使用,其中一个希望他们的员工按照内联网自动登录,因为显然必须登录会阻止他们使用它!?!

我确定您不能混合使用 Windows 和 Forms 身份验证,所以我的问题是,是否可以识别 windows/ad 用户名或其他任何东西,以便我可以手动对它们进行身份验证?我不需要通过 Windows 对它们进行实际身份验证,我只需要以某种方式获取信息,使我能够将它们与数据库中的记录相匹配。

最佳答案

我做过类似的事情。 您将需要配置 IIS 以启用 Windows 身份验证。 如果他们的 AD 帐户与他们的 Web 应用程序标识匹配(或者只是在表单例份验证数据库中为 AD id 添加一列),您可以从他们的用户名中删除域,然后根据您的身份验证数据库对其进行验证。

我有一个 HR 数据库,我们用它来跟踪员工及其职位,根据该数据库中包含的数据,我们在整个应用程序中允许某些特权:

我们公司仍在使用 Active Directory 2003,因此如果不找到创造性的解决方案就无法管理凭据。

这是我的代码: //找出谁是 Active Directory 用户 var username = Convert.ToString(User.Identity.Name);

    // strip the domain
    username = Regex.Replace(username, "DOMAIN", "");
    username = Regex.Replace(username, "\\\\", "");


    var first_initial = username[0];
    var last_initial = username[1];
    //The Ative directory structure is [first initial firstname][first inital lastname][badge number]
    var EIN = Regex.Replace(username, "[^0-9]", "");//get the badge number so we can query the badging database


    // check the employee database to see if this AD account matches something 
    var query = "SELECT BadgeNumber, FirstName, LastName,  DEPT_NUM, DEPT, TITLE, Supervisor " +
                     "FROM TABLE_WITH EMPLOYEE INFORMATION " +
                     "WHERE LEFT(FirstName, 1) = '" + first_initial + "' AND LEFT(LastName, 1) = '" + last_initial + "' AND BadgeNumber = '" + EIN + "' AND STATUS = 'Active' ";

关于c# - 可以根据 Windows 用户名自动登录 Forms 身份验证的 MVC 应用程序吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31477969/

相关文章:

c# - ASP.NET MVC4 重定向到登录页面

javascript - (SystemJS) 意外标记 < ( ASP.net + Angular 4)

c# - .Net Forms 身份验证是否需要 session ?

c# - 如何阅读 MVC OWIN AuthenticationProperties?

c# - 从 X509Certificate 对象导出私钥

c# - 如何查找坐标点是否在边界内

c# - 在 C# 中创建动态字符串数组并通过循环将字符串(拆分方法的结果)添加到两个单独的数组中

asp.net - 找不到 Windows Server 2008 R2 : Compiler executable file csc. exe

javascript - 参数字典包含不可为 null 类型 'talepID' 的参数 'System.Int32' 的 null 条目

asp.net-mvc - 响应状态码不表示成功 : 401 in the task "restore" on the azure-pipeline