SignInAsync后,C#MVC 5 User.Identity.Name为null

时间:2015-10-01 07:28:12

标签: c# asp.net-mvc login identity

我正在为我的公司开发一个MVC 5应用程序,我仍然坚持如何自定义我的登录风格。我们走了......

public async Task<ActionResult> Login(LoginViewModel model, string userType)
    {
        bool checkLDAP = AuthenticateUser("corp.mycorp.go.id", model.UserName, model.Password);
        if (ModelState.IsValid)
        {
            try
            {
                if (userType== "01")
                {
                    AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
                    var externalUser = await SignInManager.PasswordSignInAsync(model.UserName, model.Password, false, false);
                    if (externalUser==SignInStatus.Success)
                    {    
                        string idUser = ValidationHelper.GetIdUser(User.Identity.Name);                        
                        string idRole = ValidationHelper.GetRoleCode(idUser);
                        SessionHelper.SetSessionObject("ActiveRole", idRole);
                        return RedirectToAction("Index", "Home");
                    }
                    else
                    {
                        ModelState.AddModelError("", "User not registered.");
                        return View(model);
                    }

                }
                else if (userType== "02" && checkLDAP == true)
                {
                    AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
                    ApplicationUser internalUser = await UserManager.FindByNameAsync(model.UserName);

                    if (internalUser != null)
                    {
                        await SignInManager.SignInAsync(internalUser, false, 
                        string idUser = ValidationHelper.GetIdUser(User.Identity.Name);
                        string idRole = ValidationHelper.GetRoleCode(idUser);
                        SessionHelper.SetSessionObject("ActiveRole", idRole);
                        return RedirectToAction("Index", "Home");
                    }
                    else
                    {
                        ModelState.AddModelError("", "User not registered.");
                        return View(model);
                    }
                }
                else
                {
                    ModelState.AddModelError("", "Login Failed");
                    return View(model);
                }
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", ex.Message.Replace(Environment.NewLine, " "));
                return View(model);
            }
        }
        else
        {
            ModelState.AddModelError("", "Login Failed");
            return View(model);
        }
    }

如您所见,我想获取带有参数User.Identity.Name

的用户ID
string idUser = ValidationHelper.GetIdUser(User.Identity.Name);

但我得到的只是空。我该如何解决这个问题?

更新: 这是我的AuthenticateUser for userType 02

public static bool AuthenticateUser(string pDomainName, string pUserID, string pPassword)
    {
        bool result = false;
        string pServerMessage = "";
        DirectoryEntry dir = new DirectoryEntry(string.Format("LDAP://{0}", pDomainName), pUserID, pPassword);
        try
        {
            DirectorySearcher search = new DirectorySearcher(dir);
            search.Filter = "(SAMAccountName=" + pUserID + ")";
            search.PropertiesToLoad.Add("cn");
            search.FindOne();
            result = true;
        }
        catch (Exception ex)
        {
            pServerMessage = ex.Message;
        }

        return result;
    }

对于userType 01,我使用默认的RTM身份验证。

0 个答案:

没有答案