ASP.NET MVC Windows Authentiaction and DirectoryServices - Get Mail Address of the current user throws an InvalidCastException

℡╲_俬逩灬. 提交于 2019-12-04 10:10:41
Rookian

Set the IIS Application Pool Identity to NetworkService and use:

var identityName = HttpContext.Current.User.Identity.Name;
using (HostingEnvironment.Impersonate())
{
    using (var context = new PrincipalContext(ContextType.Domain, "yourDomain", null, ContextOptions.Negotiate | ContextOptions.SecureSocketLayer))
    using (var userPrincipal = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, identityName))
    {
        emailAddress = userPrincipal.EmailAddress;
        lastname = userPrincipal.Surname;
        firstname = userPrincipal.GivenName;
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!