Authentication over multiple projects

人盡茶涼 提交于 2020-01-15 09:49:28

问题


I am trying to get a single login for multiple projects.

All projects use the same DB and therefore the same login details.

I have got all the projects looking at the same login page but when one logs in it doesnt automatically log in for the others so if I then load up the second project it requires logging in again.

I'm also struggling to get the return Url to be correct.

Each project has this in the Web.Config

<authentication mode="Forms">
  <forms loginUrl="http://localhost:56131/User/Login" timeout="2880"/>
</authentication> 

The login controller looks like this:

[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);
    }

Anyone able to help?

来源:https://stackoverflow.com/questions/43705119/authentication-over-multiple-projects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!