【问题标题】:Should I Use Windows Authentication in ASP.NET 5?我应该在 ASP.NET 5 中使用 Windows 身份验证吗?
【发布时间】:2015-09-23 03:46:37
【问题描述】:

我习惯于创建内部 asp.net windows 身份验证应用程序,这些应用程序会自动填充一个 WindowsPrincipal,它声称用户名和一些我认为来自 Active Directory 的组信息。我安装了 Visual Studio 2015 并尝试使用 ASP.NET 5 Web 应用程序,但 Windows 身份验证显示为灰色。我一直在学习基于声明的身份验证,也了解 ASP.NET 5 将带来的跨平台功能,并且我可能需要摆脱 Windows 身份验证。

这个内部 Web 应用程序位于 Active Directory 域中,如果不需要,我不想让用户提供凭据,因为这将在 IIS 之上进行。我应该如何应该获取当前用户?我是否需要选择不同的身份验证方法并以某种方式通过另一种方法从 AD 获取声明,或者 Windows 身份验证仍然是这种情况下的推荐选项并且最终将可用?

【问题讨论】:

    标签: asp.net windows-authentication


    【解决方案1】:

    我还没有玩过 v5,但我在我的 Intranet 应用程序中使用 Windows 身份使用 SSO。你试过 GenericPrincipal 吗?现在我很好奇,需要与我的雇主谈谈测试 .NET 5...

    使用 GenericPrincipal 和 IIdentity,您可以从登录 Windows 的用户的活动目录中获取任何内容。

    这些都在我的 Employee 类中。

    public Employee(Authenticate identity)
    {
        if (identity.IsAuthenticated)
        {
            // if the account exists, build the employee object
            loadEmployee(identity as IIdentity);
        }
        else
        {
            // If the account cannot be found in Active Directory, throw an exception
            throw new ArgumentOutOfRangeException(identity.Name, "Employee does not exist");
        }
    }
    
    private void loadEmployee(IIdentity identity) 
    { 
        // the domain name of the identity
        string domain = identity.Name.Split('\\')[0];
    
        // the username of the identity
        string username = identity.Name.Split('\\')[1];
    
        // Initializes a new context for the identity's domain
        PrincipalContext domainContext = new PrincipalContext(ContextType.Domain, domain);
    
        // Initialize a principal using the domain context and the username of the identity
        UserPrincipal user = UserPrincipal.FindByIdentity(domainContext, username);
    
        // Build a collection of all the underlying objects
        var userProperties = ((DirectoryEntry)user.GetUnderlyingObject()).Properties;
    
        // populate the employee objects properties
        LastName = user.Surname;
        FirstName = user.GivenName;
        NtUserName = username;
        GUID = user.Guid.ToString();
    }
    

    在我的 AuthenticationFilter 类中,我构建了我的 IPrincipal。

    //build the system's Identity and Principal
    var genIdentity = new GenericIdentity(employee.FirstName + " " + employee.LastName);
    var genPrincipal = new GenericPrincipal(genIdentity, roles[]);
    var identity = (IPrincipal)genPrincipal;
    context.User = identity;
    

    希望这符合您的要求。

    【讨论】:

    • 感谢您的回复,我现在在 4.5 中做了类似的事情,并在收到来自 Windows Auth 的初始身份后构建自定义身份。我没有使用 SSO。我今天可以使用 hacking asp.net 5 来​​尝试让 windows auth 工作,但我想了解我是否应该朝着另一个方向前进,或者是否会内置支持。
    • 更新:我从新项目对话框窗口中选择了“无身份验证”,然后在“调试”选项卡 IIS Express 设置下的项目属性中,我取消选中匿名身份验证并选中 Windows 身份验证。之后,Controller 类中可用的新 User 和 Context.User 属性会自动填充来自 AD 的声明,在让 IIS 将用户的域凭据传递给应用程序之前,我经常看到这些声明。我假设我仍然可以拦截管道以在此之上创建自定义声明,但我使用 AD 对吗?是否有我没有使用的 AD 中的身份特征?
    • 我刚刚浏览了 GitHub 的 dotnet 项目,找不到 System.DirectoryServices 程序集,这告诉我它还不支持。此 SO 响应支持该想法。 stackoverflow.com/a/28021962/755908。因此,即使您为 Windows 身份验证设置项目,您也无法使用 .Net 5 中的 DirectoryServices 使用自定义声明。但是,一旦启用,您将能够使用 Active Directory 实现 SSO。
    猜你喜欢
    • 1970-01-01
    • 2020-11-30
    • 2016-08-25
    • 2023-03-19
    • 2011-06-27
    • 2019-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多