asp.net mvc 中的原理上下文返回空值

迪利普·普拉贾帕特

我正在尝试使用原则上下文与活动目录连接。我试过下面的代码。

using (var context = new PrincipalContext(ContextType.Domain,
                        ConfigurationManager.AppSettings["DomainName"].ToString()))
                    {
                        try
                        {
                            writeLog("Before:" + isCheckUserName);
                            writeLog("Context name:" + context.Name);
                            var user = UserPrincipal.FindByIdentity(context, GetCurrentWindowsLogin());

                            writeLog("GetCurrent:" + GetCurrentWindowsLogin());
                            writeLog("After:" + user.EmployeeId);
                            if (user != null) {
                                StaffName = user.DisplayName;
                                StaffID = user.EmployeeId;

                            }

                        }
                        catch (Exception ex)
                        {
                            writeLog($"Second try: Error - {ex.Message} Inner Exception: {ex.InnerException.Message}");
                        }
                    }

这段代码在客户端的本地机器上工作正常,但在客户端的服务器上上传后,它会抛出一个空引用异常。

任何想法。谢谢。

迪利普·普拉贾帕特

我自己解决了这些。我在下面的 PrincipleContext 构造函数中添加了另外两个参数。即 Active Directory 域用户名和密码。

using (var context = new PrincipalContext(ContextType.Domain,
                    ConfigurationManager.AppSettings["DomainName"].ToString(),
                    ConfigurationManager.AppSettings["ADUserName"].ToString(),
                    ConfigurationManager.AppSettings["ADPassword"].ToString()))
                {
                    try
                    {
                        writeLog("Before:" + isCheckUserName);
                        writeLog("Context name:" + context.Name);
                        var user = UserPrincipal.FindByIdentity(context, GetCurrentWindowsLogin());

                        writeLog("GetCurrent:" + GetCurrentWindowsLogin());
                        writeLog("After:" + user.EmployeeId);
                        if (user != null)
                        {
                            StaffName = user.DisplayName;
                            StaffID = user.EmployeeId;

                        }

                    }
                    catch (Exception ex)
                    {
                        writeLog($"Second try: Error - {ex.Message} Inner Exception: {ex.InnerException.Message}");
                    }
                }

并在 IIS 授权中更改。将匿名身份验证设置为“禁用”,并将 ASP.NET 模拟设置为“启用”。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章