【发布时间】:2014-10-02 07:30:36
【问题描述】:
我需要实现一个从 AD 中检索一些字段的类。我已按照此处的说明进行操作:link。但是我从行中得到了一个未处理的异常:
return (UserPrincipalExtended)FindByIdentityWithType(context, typeof(UserPrincipalExtended), identityValue)
我的控制器代码如下所示:
using System.Threading.Tasks;
using System.Net;
using System.Data.Entity;
using System.DirectoryServices.AccountManagement;
(...)
public class HomeController : Controller
{
private FormsEntities db = new FormsEntities();
public async Task<ActionResult> Index()
{
UserPrincipalExtended user = UserPrincipalExtended.FindByIdentity(
new PrincipalContext(ContextType.Domain), User.Identity.Name);
var title = user.Title;
ViewBag.Message = title;
return View();
}
我应该添加任何其他配置吗?以web.config 为例?顺便提一下,我用AD登录名和密码以及代码成功实现了Windows身份验证。
string userName = HttpContext.User.Identity.Name.Split('\\')[1].ToString();
显示正确的用户。我还尝试操作对象类型[DirectoryObjectClass("user")],但没有任何结果。
【问题讨论】:
-
我检查过 if (null == UserPrincipalExtended.FindByIdentity(new PrincipalContext(ContextType.Domain), User.Identity.Name)) { throw new Exception("Some exception"); } 但异常仍然发生
标签: c# asp.net asp.net-mvc asp.net-mvc-4 active-directory