Get current user's email address in .NET

前端 未结 4 1533
面向向阳花
面向向阳花 2021-02-02 07:24

I would like to know the email address of the user (assuming she\'s in a typical Windows office network). This is in a C# application. Perhaps something to the effect of

4条回答
  •  北荒
    北荒 (楼主)
    2021-02-02 07:43

    I didn't want to use the Active Directory option and the other, most selected answer, did not work for me oddly enough.

    I searched my code bank and found this which worked fine and with quick response:

    using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "[domain]",  dc=xx,dc=yyy"))
    {
        UserPrincipal cp = UserPrincipal.FindByIdentity(ctx, Environment.UserName);
        userEmail = cp.EmailAddress;
    }
    

提交回复
热议问题