Get current user's email address in .NET

前端 未结 4 1537
面向向阳花
面向向阳花 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:48

    Reference System.DirectoryServices.AccountManagement, then

    using System.DirectoryServices.AccountManagement;
    UserPrincipal.Current.EmailAddress
    

    Or with a timeout:

    var task = Task.Run(() => UserPrincipal.Current.EmailAddress);
    if (task.Wait(TimeSpan.FromSeconds(1)))
        return task.Result;
    

提交回复
热议问题