使用 ASP.NET MVC 进行 Windows 身份验证

发布于 2024-08-04 05:14:00 字数 373 浏览 2 评论 0原文

我为我的 asp.net mvc 1.0 Web 应用程序构建了一个自定义登录系统,因为我为每个用户存储大量用户数据(因此我决定不尝试为 Windows 身份验证添加自定义表)。登录系统基本上使用SQL Server(2005或2008)和我自己的数据库和表结构,这是相当标准的。具有唯一 ID、用户名和哈希密码的用户表,该表链接到我的其他用户相关数据表。

我的问题是,如何将我的系统绑定到使用 Windows 身份验证登录。我想允许管理员为用户(在我的系统中定义)选择 Windows 身份验证登录名,并可能向我的自定义表中的某些内容添加一个值,以便我可以用来对他们进行身份验证?

这个问题可能措辞错误,我可能误解了 Windows 身份验证的工作原理,但我想在我的 Web 应用程序中提供该选项。

I've built a custom login system for my asp.net mvc 1.0 web application as I store large amounts of user data for each user (I decided against trying to add custom tables for the windows authentication due to this). The login system basically uses SQL Server (2005 or 2008) and my own database and table structure which is pretty standard. A users table with an unique id, username and hashed password which is linked to my other tables of user related data.

My question is, how can I tie my system to use Windows Authentication logins. I would like to allow the administrator to for a user (as defined in my system) select a Windows Authentication login and perhaps add a value to something in my custom table that I can use to authenticate them?

The question is probably phrased wrong and I might have misunderstood how Windows Authentication works but I would like to offer the option in my web application.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

若水微香 2024-08-11 05:14:00

如果您的站点上启用了 Windows Auth,那么您应该能够使用 User.Identity.Name 获取当前登录用户的 NT/Active Directory 用户名,并将其与用户表中的列进行匹配。

If you have Windows Auth enabled on your site then you should be able to use User.Identity.Name to get their NT/Active Directory user name of the currently logged in user, and match that to a column in your users table.

遗忘曾经 2024-08-11 05:14:00

以下是我们为混合表单/Windows 身份验证应用程序所做的操作:-

public class MyBaseController
{
  protected override void OnAuthorization( AuthorizationContext authContext )
  {
    if
    (
      !User.Identity.IsAuthenticated &&
      Request.LogonUserIdentity != null &&
      Request.LogonUserIdentity.IsAuthenticated
    )
    {
      String logonUserIdentity = Request.LogonUserIdentity.Name;
      if ( !String.IsNullOrEmpty(logonUserIdentity) )
      {
        User loginUser =
          Context.Users.FirstOrDefault(
            x => x.UserIdentity == logonUserIdentity);
        if ( loginUser != null )
          FormsAuthentication.SetAuthCookie(
            loginUser.LoginName,createPersistentCookie);
    }
  }

为了紧凑性,我删除了一些封装。

Here's how we've done it for a hybrid forms/windows authentication app:-

public class MyBaseController
{
  protected override void OnAuthorization( AuthorizationContext authContext )
  {
    if
    (
      !User.Identity.IsAuthenticated &&
      Request.LogonUserIdentity != null &&
      Request.LogonUserIdentity.IsAuthenticated
    )
    {
      String logonUserIdentity = Request.LogonUserIdentity.Name;
      if ( !String.IsNullOrEmpty(logonUserIdentity) )
      {
        User loginUser =
          Context.Users.FirstOrDefault(
            x => x.UserIdentity == logonUserIdentity);
        if ( loginUser != null )
          FormsAuthentication.SetAuthCookie(
            loginUser.LoginName,createPersistentCookie);
    }
  }

There's some encapsulation that I've taken out for the sake of compactness.

蒗幽 2024-08-11 05:14:00

如果我正确理解您的问题,您想添加一些链接到 Windows 身份验证用户名的其他数据吗?

如果是这样,您将需要将用户名和此自定义信息存储在新表中。 Windows 身份验证数据存在于 Active Directory 中,因此您可以在其中查看以获取用户列表。当 Windows 对用户进行身份验证时,您不会自动将任何自定义信息添加到 AD。如果您需要任何自定义信息,则需要在 AD 中添加自定义查找,或者仅在数据库中查找自定义数据,具体取决于您决定存储信息的位置。

通过 Windows 身份验证,您获得的几乎所有内容都是用户的用户名以及检查与该用户关联的角色(AD 组)的能力。除此之外,您将需要手动编码。

最近,我询问了如何在 MVC 中内置安全性之外实现自定义,并自行提出了一个解决方案。也许有一些花絮可以帮助您回答您的问题:

如何在ASP.NET MVC中基于Session数据实现授权检查?

If I am understanding your question correctly you want to add some other data linked to a Windows Authenticated user name?

If so you will need to store the username and this custom information in a new table. The windows authentication data exists in Active Directory so you could look there to get a list of users. You will not get any custom information added to AD automatically when Windows authenticates the user. If you want any custom info you will need to add a custom lookup into AD for it or just lookup your custom data in your database depending on where you decide to store the information.

Pretty much all you get with the Windows Authentication is the user's username and the ability to check the roles (AD groups) associated with that user. Anything beyond that you will need to manually code up.

I recently asked about implementing customization beyond the built in security in MVC and came up with a solution on my own. Maybe there is some tidbits that might help you answer your question:

How to implement authorization checks in ASP.NET MVC based on Session data?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文