服务器名称不能包含空格字符

时间:2020-05-28 12:24:15

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

我有一个MVC5应用程序,用户必须使用域凭据登录。当我对域变量的值进行硬编码时,应用程序登录就没有问题。出于安全原因,我已经在数据库表中设置了变量值,但是当我尝试使用Linq进行检索时,出现错误消息服务器名称不能在此行包含空格字符:

    PrincipalContext ctx = new PrincipalContext(ContextType.Domain, domainControllerService.GetDomain());

具有堆栈跟踪:

    at System.DirectoryServices.Protocols.LdapDirectoryIdentifier..ctor(String[] servers, Boolean 
    fullyQualifiedDnsHostName, Boolean connectionless)
       at System.DirectoryServices.Protocols.LdapDirectoryIdentifier..ctor(String server)
       at System.DirectoryServices.Protocols.LdapConnection..ctor(String server)
       at System.DirectoryServices.AccountManagement.PrincipalContext.ReadServerConfig(String 
    serverName, ServerProperties& properties)           at 
    System.DirectoryServices.AccountManagement.PrincipalContext.DoServerVerifyAndPropRetrieval()
       at System.DirectoryServices.AccountManagement.PrincipalContext..ctor(ContextType contextType, 
    String name, String container, ContextOptions options, String userName, String password)
    at System.DirectoryServices.AccountManagement.PrincipalContext..ctor(ContextType contextType, 
    String name)
       at FBChecklist.Controllers.UserController.Login(DomainControllerViewModel model) in                             
      C:\Users\tshumae.FBC\source\repos\FBCHECKLIST\FBChecklist\Controllers\UserController.cs:line 49

这是使用硬编码值的登录代码(有效):

    public ActionResult Login()
    {
        var model = new DomainControllerViewModel();
       // LoginViewModel model = new LoginViewModel();
        return View(model);
    }

    // POST: User/Delete/5
    [HttpPost]
    public ActionResult Login(DomainControllerViewModel model)
    {
        try
        {
          PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "MYDOMAIN.CORP");              
            // find a user
            UserPrincipal user = UserPrincipal.FindByIdentity(ctx, model.Username);
            if (user != null)
            {
                // check user lockout state
                if (user.IsAccountLockedOut())
                {
                    ViewBag.Message = "Your account is locked out";
                }
                else
                {
                    bool authentic = false;
                    try
                    {
                        DirectoryEntry entry = new DirectoryEntry("LDAP://XX.XX.XX.XX:XXX/OU=YYY,DC=YYY,DC=corp",, model.Username, model.Password);
                        DirectoryEntry ldapConnection = new DirectoryEntry("MYDOMAIN.CORP");
                        ldapConnection.Path = "LDAP://";
                        ldapConnection.Username ="myusername";
                        ldapConnection.Password = "mypassword";
                        ldapConnection.AuthenticationType = AuthenticationTypes.Secure;
    ....
    ....

                return View();
    }

并且使用(不起作用)从数据库即时通讯中检索:

    [HttpPost]
    public ActionResult Login(DomainControllerViewModel model)
    {
        try
        {
           PrincipalContext ctx = new PrincipalContext(ContextType.Domain, domainControllerService.GetDomain());
      ...
    ...                           
                    try
                    {

                        DirectoryEntry entry = new DirectoryEntry(domainControllerService.GetDirectoryEntry(), model.Username, model.Password);
                        DirectoryEntry ldapConnection = new DirectoryEntry(domainControllerService.GetDomain());
                        ldapConnection.Path = "LDAP://";
                        ldapConnection.Username =domainControllerService.GetUsername();
                        ldapConnection.Password = domainControllerService.GetPassword();
                        ldapConnection.AuthenticationType = AuthenticationTypes.Secure;
   ...
    ...
    return View();
    }

存储库(DomainControllerService.cs):

    public string GetDomain()
   {
       var domain = (from j in appEntities.DomainControllers
                     select new
                     {
                         j.Domain
                     });
       return domain.ToString();
   }

我尝试使用下面的变体删除空格,但是我仍然遇到相同的异常:

     public string GetDomain()
   {
       var domain = (from j in appEntities.DomainControllers
                     select new
                     {
                         j.Domain
                     });
       string domaintostring = domain.ToString();
       string dom = Helpers.RemoveWhitespace(domaintostring);
       return dom;
   }

通过扩展方法:

    public static string RemoveWhitespace(string input)
    {
        return new string(input.ToCharArray()
            .Where(c => !Char.IsWhiteSpace(c))
            .ToArray());
    }

0 个答案:

没有答案