0
我正在构建一个MVC5站点,用于仅限Intranet的设置。由于员工在工作时共享电脑,因此我需要将登录/注销功能纳入网站。该应用程序将针对Active Directory进行身份验证。告诉MVC5用户已通过Active Directory登录/注销
我有认证在这里工作。但是,当页面将用户带回到returnUrl时,User.Identity.IsAuthenticated
和Request.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代码创建一个标准的互联网登录该方法。我试图找出如何生成一个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);
}
您可以使用HttpUnauthorizedResult在MVC中返回401。 –
您可以使用Response.AppendHeader添加响应头 –