我正在尝试为多个项目进行单一登录。
所有项目都使用相同的数据库,因此使用相同的登录详细信息。
我已经让所有项目查看同一个登录页面,但是当一个登录时它不会自动登录其他项目,所以如果我加载第二个项目则需要再次登录。
我也在努力让返回的网址正确无误。
每个项目都在Web.Config
中<authentication mode="Forms">
<forms loginUrl="http://localhost:56131/User/Login" timeout="2880"/>
</authentication>
登录控制器如下所示:
[AllowAnonymous]
public ActionResult Login(string returnUrl)
{
if (Request.UrlReferrer != null)
{
returnUrl = Server.UrlEncode(Request.UrlReferrer.AbsolutePath);
}
var model = new NewUserModel();
TempData["ReturnUrl"] = returnUrl;
return View(model);
}
[HttpPost]
[AllowAnonymous]
public ActionResult Login(UserModel model, string returnUrl)
{
SpoakEntities ctx = new SpoakEntities();
if (ModelState.IsValid)
{
string Identity = model.UserName;
string password = model.Password;
try
{
var User = (from u in ctx.Users
where u.UserName == model.UserName
select u).SingleOrDefault();
bool userValid = ctx.Users.Any(user => user.EmailAddress == Identity || user.UserName == Identity) && Crypto.VerifyHashedPassword(User.Password, password);
//bool userValid = ctx.Users.Any(user => user.EmailAddress == Identity || user.UserName == Identity) && User.Password == password;
//if (userValid && WebSecurity.Login(Identity, password))
if (userValid)
{
//TODO: Use ControllerContext to redirect to the correct place
FormsAuthentication.SetAuthCookie(User.Guid.ToString(), false);
var authTicket = new FormsAuthenticationTicket(1, User.Guid.ToString(), DateTime.Now, DateTime.Now.AddMinutes(200), true, User.Role.ToString());
string encryptedTicket = FormsAuthentication.Encrypt(authTicket);
var authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
HttpContext.Response.Cookies.Add(authCookie);
if (Url.IsLocalUrl(returnUrl))
{
return Redirect(returnUrl);
}
else
{
return RedirectToAction("Index", "Home");
}
}
else
{
ModelState.AddModelError("", "The user name or password provided is incorrect.");
return View();
}
}
catch (Exception ex)
{
ModelState.AddModelError("", "The user name or password provided is incorrect.");
return View();
}
}
return View(model);
}
任何人都可以提供帮助吗?