我有两个项目,MVC Web应用程序和Web服务。 在MVC网络应用程序中,我有一个名为“帐户”的控制器。 在“帐户”中有一个名为“登录”的操作。 'Login'action从请求中获取Authentication Manager。 “登录”操作调用Web服务以识别用户是否存在。 如果用户存在,则Web服务将为该特定用户返回“ClaimsIdentity”。 然后,“Login”操作将使用身份验证管理器和声明标识来签署用户。
此后,用户未进行签名/身份验证。我知道这一点,因为User.Identity.Name为空,User.Identity.IsAuthenticated为false。需要解释为什么会发生这种情况以及我可以采取哪些措施来解决问题。没有构建时或运行时错误。我需要Web服务来执行对数据库的所有调用。
帐户登录代码
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult LogIn(LoginModel model)
{
if (!ModelState.IsValid)
{
return View(model);
}
var authManager = GetAuthenticationManager();
var loginFeedback = _webService.Login(model);
if (loginFeedback.Success)
{
authManager.SignIn(loginFeedback.Identity);
return RedirectToAction("Index", "Home");
}
else
{
ModelState.AddModelError("", loginFeedback.FeedbackMessage);
return View(model);
}
}
GetAuthenticationManager
private IAuthenticationManager GetAuthenticationManager()
{
var context = Request.GetOwinContext();
return context.Authentication;
}
网络服务登录
public LoginFeedbackDto Login(LoginModel loginModel)
{
var userManager = GetUserManager();
var user = userManager.Find(loginModel.Email, loginModel.Password);
var dto = new LoginFeedbackDto();
string status = user.Status.ToLower();
if (status == "invalid")
{
dto.Success = false;
dto.FeedbackMessage = @"This account has not been activated.";
}
else if (status == "rejected")
{
dto.Success = false;
dto.FeedbackMessage = @"This account has been rejected.";
}
else if (status != "invalid" && status != "rejected" && status != "processed")
{
dto.Success = false;
dto.FeedbackMessage = @"We are unable to log you in due to a technical issue.";
}
else
{
dto.Success = true;
dto.Identity = userManager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);
}
userManager.Dispose();
return dto;
}
答案 0 :(得分:1)
这里发生的是您正在创建的身份在RedirectToAction上“丢失”
您可以使用CookieMiddleware,它可以创建代表当前用户的cookie
你需要一个Owin启动类,它需要类似下面的代码。
确保'SelfIssue'与您的Web服务将ClaimsIdentity.AuthenticationType设置为相同的AuthenticationType相同。
expr -> string
expr -> ' string '
expr -> " string "