【问题标题】:C# Active Directory authenticationC# Active Directory 身份验证
【发布时间】:2013-07-15 20:27:57
【问题描述】:

我在 AngularJS 中有一个繁重的客户端 web 应用程序。我正在使用 C# 控制器进行 Active Directory 身份验证,这是这段代码。

public UserModel Get()
{
        UserModel currentUser = new UserModel();
        currentUser.name = User.Identity.Name;
      //  currentUser.auth = contactFromAD(User.Identity.Name);
        currentUser.auth = true;
        return currentUser;
}

所以它所做的是检查您登录的 AD 帐户,然后执行一些逻辑来检查您是否已通过身份验证访问该站点。

但是我怎样才能让它在本地主机之外工作。当代码运行服务器时,我怎样才能让User.Identity.Name 返回当前使用该应用程序的人的身份。

【问题讨论】:

    标签: c# asp.net-mvc active-directory


    【解决方案1】:

    我不确定我是否理解 AngularJS 角度。如果我有常规的 WCF 服务,我会使用 ServiceSecurityContext.Current 中的 WindowsIdentity。
    http://msdn.microsoft.com/en-us/library/system.servicemodel.servicesecuritycontext.aspx

    ServiceSecurityContext securityContext = ServiceSecurityContext.Current;
    
    if (securityContext == null)
         throw new Exception("Failed to retrieve Service Security Context");
    
    WindowsIdentity identity = securityContext.WindowsIdentity;
    currentUser.name = identity.Name
    

    【讨论】: