告诉用户通过Active Directory登录/退出的MVC5

时间:2013-11-14 18:31:13

标签: asp.net-mvc login active-directory asp.net-mvc-5

我正在构建一个仅在Intranet设置中使用的MVC5站点。由于员工在工作时共享PC,我需要在站点中加入登录/注销功能。该应用程序将针对Active Directory进行身份验证。

我的身份验证工作正常。但是,当页面将用户带回returnUrl时,User.Identity.IsAuthenticatedRequest.IsAuthenticated都是假的。这导致主页仍然提供“登录”选项,即使他们之前已经成功完成了该动作。

如何告诉MVC用户已成功登录?

来自Web.config:

<authentication mode="Windows" />

来自帐户控制器:

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
    if (ModelState.IsValid)
    {
        using (var pc = new PrincipalContext(ContextType.Domain))
        {
            if (pc.ValidateCredentials(model.UserName, model.Password, ContextOptions.Negotiate | ContextOptions.SimpleBind))
            {
                FormsAuthentication.SetAuthCookie(model.UserName, false);
                return RedirectToLocal(returnUrl);
            }
            else
            {
                ModelState.AddModelError("", "Invalid username or password.");
            }
        }
    }

更新 默认的MVC代码为标准Internet登录创建此方法。我试图找出如何生成ClaimsIdentity对象,以便我也可以使用它。

private async Task SignInAsync(ApplicationUser user, bool isPersistent)
{
    AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
    var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
    AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity);
}

1 个答案:

答案 0 :(得分:0)

我不确定你是否正确地进行了ntlm身份验证。 基本上你可以添加“登录为不同用户”按钮到你的站点和事件处理程序,它将返回到用户401响应,反过来将触发客户端上的Windows登录弹出窗口输入凭据。 请查看this帖子。

相关问题