Active Directory UserPrincipal问题

时间:2017-01-13 16:25:18

标签: c# active-directory

我有一些代码可以根据当前上下文获取用户,这种情况发生在我的应用程序的Page_Load()方法中。

var context = new PrincipalContext(ContextType.Domain, "dc", "DC=domain,DC=com", "user", "password");
UserPrincipal user = UserPrincipal.FindByIdentity(context, User.Identity.Name);
string Name = user.Name.Trim();
lblName.Text = Name;

这样做并且标签正确显示登录用户(Windows身份验证)。

但是,我的代码的另一部分,在按下按钮时,会在包含其他用户名的数据网格中循环,并使用类似的方法来获取有关这些用户的信息。

我在页面上使用了标签来显示相关的用户信息,但其中一个标签似乎根据它在循环中处理的用户而发生变化,而实际上只需要参考当前记录的用户在用户中。

有解决方法吗?我认为通过使用基本上做同样事情的单独方法,我会得到不同的对象。返回值不同,所以我不确定这是怎么回事。

下面的相关代码,附有一些注释,用于解释或指出代码不相关的位置(发送电子邮件和处理异常 - 全部删除)。

public string getUserEmail()
{
var context = new PrincipalContext(ContextType.Domain, "dc", "DC=domain,DC=com", "user", "password"); ;
UserPrincipal user = UserPrincipal.FindByIdentity(context, User.Identity.Name);
string email = user.EmailAddress;
return email;
}

public string getOtherUserEmail(string user)
{
user = user.Trim();
var context = new PrincipalContext(ContextType.Domain, "dc", "DC=domain,DC=com", "user", "password");
UserPrincipal u = UserPrincipal.FindByIdentity(context, user);
string email2= u.EmailAddress;
return email2;
}

protected void btnEmail_Click(object sender, EventArgs e)
{
    foreach (GridViewRow row in gdView1.Rows)
    {
        if (row.RowType == DataControlRowType.DataRow)
        {
            Label FullName = gdView1.Rows[index].FindControl("lblFullName") as Label; // other username from gridview
            string FromAddress = getUserEmail(); //logged in user
            try
            {
                string otherName = FullName.Text.ToString(); 
                string ToAddress1 = getOtherUserEmail(FullName); //Other user
                //Generate an email message....
                smtpClient.Send(mail);
                lblMessage.Text = "Email sent!";
            }
            catch
            { // exception handling
            }
    }
}

protected void gdView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            TableCell cell = gdView1.Rows[e.RowIndex].Cells[2]; //Cell containing "other" user name
            Message.Text = cell.Text;
            string otherName = cell.Text.ToString();
            string ToAddress = getOtherUserEmail(otherName); //Other user
            string FromAddress = getUserEmail(); //logged in user
            try
            { //email as above
            }
            catch
            { // exception handling
            }
    }

1 个答案:

答案 0 :(得分:2)

根据您发布的内容,我希望您也可以获得不同的对象。也许在你还没有包含的代码中发生了什么?

在旁注中,对于当前登录的用户,您应该在_loggedOnUser之外定义一个字段(称为Page_Load())。这样所有方法都可以访问它。这样可以使您的代码更容易理解,甚至可以解决您的参考问题。

public partial class Page1 {

    UserPrincipal _loggedOnUser;

    public void Page_Load() {
        var context = new PrincipalContext(ContextType.Domain, "dc", "DC=domain,DC=com", "user", "password");
        _loggedOnUser= UserPrincipal.FindByIdentity(context, User.Identity.Name);
        string Name = _loggedOnUser.Name.Trim();
        lblName.Text = Name;
    }

}

然后你可以在课程的其他部分引用它,而且你不必浪费时间回到你的循环中的Active Directory。

因此,这一行:string FromAddress = getUserEmail();

可以更改为string FromAddress = _loggedOnUser.EmailAddress;

并且您可以避免另外一次昂贵的AD调用来获取您已有的信息。

相关问题