【问题标题】:Trying to read FormsAuthentication tickets to read in other areas of site尝试阅读 FormsAuthentication 票证以在网站的其他区域阅读
【发布时间】:2023-03-29 14:33:01
【问题描述】:

注意:我在此处包含 3 个指向本地主机区域的链接,但无法提交帖子,因此我用空格字符分隔它们,以便它会发布在 stackoverflow 上。

我的解决方案中目前有 2 个 ASP.NET MVC 应用程序。首先,我通过将其设置为启动项目来运行第一个。它进入登录页面,输入数据后,我执行以下代码:

var authTicket = new FormsAuthenticationTicket(1, login.LoginDataContract.MSISDN, DateTime.Now,
                                                           DateTime.Now.AddMinutes(Convert.ToDouble("30")), true, "");

            string cookieContents = FormsAuthentication.Encrypt(authTicket);

            var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, cookieContents)
                             {
                                 Expires = authTicket.Expiration,
                                 //Path = FormsAuthentication.FormsCookiePath
                                 //Path = "http://localhost"
                                 Domain = ""
                             };

            if (System.Web.HttpContext.Current != null)
            {
                System.Web.HttpContext.Current.Response.Cookies.Add(cookie);
            }

正如你所看到的,我已经设置了 Domain = "",所以理论上它应该适用于我的 http://localhost 下的任何东西。然后我将 cookie 的持久安全性设置为 true,这样我就可以从 localhost 下的任何位置访问它。

cookie 写得很好,我现在登录了,一切都好。顺便说一句,这个登录页面的 url 是:http //localhost/MyAccount/Login

然后我停止解决方案并将其他 MVC 应用程序设置为启动。然后我运行它。第二个站点的 URL 是:http://localhost/WebActivations/ 以下是其他应用启动控制器中的代码:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        ViewData["Message"] = "Welcome to ASP.NET MVC!";

        // PASHA: Added code to read the authorization cookie set at
        // login in MyAccount *.sln

        for (int i = 0; i < System.Web.HttpContext.Current.Request.Cookies.Count;i++)
        {
            Response.Write(System.Web.HttpContext.Current.Request.Cookies[i].Name + " " + System.Web.HttpContext.Current.Request.Cookies[i].Value);
        }

        HttpCookie authorizationCookie = System.Web.HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName.ToString()];

        // decrypt.
        FormsAuthenticationTicket authorizationForm = FormsAuthentication.Decrypt(authorizationCookie.Value);

        ViewData["Message"] = authorizationForm.UserData[0].ToString();

        return View();
    }

    public ActionResult About()
    {
        return View();
    }

问题出在这个 Home 控制器中,当我运行解决方案时它无法读取身份验证 cookie,您会看到那里的循环它没有找到 .ASPXAUTH cookie。

但是一旦它在 Firefox 中崩溃,我会查看页面信息,然后查看安全性和 Cookies 以及它的存在和相同的 cookie。

我做错了什么?

【问题讨论】:

    标签: c# asp.net-mvc authentication authorization


    【解决方案1】:

    尝试设置Domain="localhost"

    【讨论】:

    • 我在两个站点的 web.configs 中都不好,我忘了添加 元素。
    • 我在那个该死的问题上工作了大约 14 个小时,那个该死的机器钥匙解决了它。我爱你兄弟:)
    【解决方案2】:

    两个网站的 web.config 都不好,我忘了添加 mahineKey 元素。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-23
      • 1970-01-01
      • 2016-11-05
      • 2022-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多