c# - ASP.NET MVC Windows Authentiaction 和 DirectoryServices - 获取当前用户的邮件地址引发 InvalidCastException

标签 c# asp.net-mvc windows-authentication directoryservices userprincipal

我正在使用 ASP.NET MVC 4 和 Windows 身份验证。 当我使用 VisualStudio 时一切正常,但当我部署我的站点时抛出异常。

var emailAddress = UserPrincipal.Current.EmailAddress;

抛出:

Unable to cast object of type 'System.DirectoryServices.AccountManagement.GroupPrincipal' to type 'System.DirectoryServices.AccountManagement.UserPrincipal'.

其余工作正常。用户可以进行身份​​验证,我可以获取用户名等。

编辑:

我在 IIS 上启用了模拟。 现在我得到以下异常:

[DirectoryServicesCOMException (0x80072020): An operations error occurred. ] System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail) +781 System.DirectoryServices.DirectoryEntry.Bind() +44 System.DirectoryServices.DirectoryEntry.get_AdsObject() +42 System.DirectoryServices.PropertyValueCollection.PopulateList() +29
System.DirectoryServices.PropertyValueCollection..ctor(DirectoryEntry entry, String propertyName) +119
System.DirectoryServices.PropertyCollection.get_Item(String propertyName) +163
System.DirectoryServices.AccountManagement.PrincipalContext.DoLDAPDirectoryInitNoContainer() +535649 System.DirectoryServices.AccountManagement.PrincipalContext.DoDomainInit() +51 System.DirectoryServices.AccountManagement.PrincipalContext.Initialize() +141 System.DirectoryServices.AccountManagement.PrincipalContext.get_QueryCtx() +42 System.DirectoryServices.AccountManagement.Principal.FindByIdentityWithTypeHelper(PrincipalContext context, Type principalType, Nullable`1 identityType, String identityValue, DateTime refDate) +27
System.DirectoryServices.AccountManagement.Principal.FindByIdentityWithType(PrincipalContext context, Type principalType, IdentityType identityType, String identityValue) +146
System.DirectoryServices.AccountManagement.UserPrincipal.FindByIdentity(PrincipalContext context, IdentityType identityType, String identityValue) +44
System.DirectoryServices.AccountManagement.UserPrincipal.get_Current() +390 Jericho.MVC.HtmlHelperExtensions.GetUser(HtmlHelper htmlHelper) in C:\Development\Jericho\Jericho.MVC\HtmlHelperExtensions.cs:48

我能做什么?

最佳答案

IIS Application Pool Identity 设置为 NetworkService 并使用:

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;
    }
}

关于c# - ASP.NET MVC Windows Authentiaction 和 DirectoryServices - 获取当前用户的邮件地址引发 InvalidCastException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10846909/

相关文章:

c# - 跳过潜在的空值 c#

asp.net-mvc - 我如何使用 linq 翻页到我的列表

ios - ios 中的 Windows azure 事件目录身份验证

asp.net-mvc-4 - IE 10 Windows 身份验证中的异步 KendoUI 上传

c# - 最佳实践 - 将字符串的第一个字符转换为小写

c# - web应用之间通信,1个SSL证书,其他没有

c# - 如何将网络摄像头捕获的帧坐标投影到屏幕坐标

c# - 查找轮廓的边界框

html - 设置 DropDownListFor 的字体大小

c# - 避免客户端> Web服务> SQL Server双跳的解决方案