【问题标题】:How do you authenticate a cookie from an old VB.Net site in an ASP.Net Core site?如何在 ASP.Net Core 站点中验证来自旧 VB.Net 站点的 cookie?
【发布时间】:2016-07-20 20:18:59
【问题描述】:

我需要在我闪亮的新 ASP.Net Core Web API + Angular2 项目中使用由旧 VB.Net aspx 项目创建的身份验证 cookie。我对基于 cookie 的授权和身份验证非常陌生,我公司中没有人知道如何将他们知道的内容转化为我正在尝试做的事情。

几天的研究和修修补补让我走到了这一步: 现在,我可以在任何需要的地方设置 [Authorize] 属性并阻止访问返回通常的 401 错误,但我的网站似乎没有将他的网站创建的 cookie 识别为该锁的众所周知的钥匙......

Startup.cs - 我没有在“Configure()”方法之外做任何事情,我应该这样做吗?

我正在将站点配置为使用 cookie 身份验证,将方案设置为“Cookies”,但不知道具体是做什么的,并将“CookieName”设置为从该旧站点登录创建的身份验证 cookie 的名称。

 public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
          {
              // Stuff eliminated for brevity

              // Set up pipeline
              app.UseDefaultFiles();
              app.UseStaticFiles();
              //app.UseIdentity(); (Don't think I need this??)

            app.UseCookieAuthentication(new CookieAuthenticationOptions()
            {
                AuthenticationScheme = "Cookies", //(Dont actually know what this does. Should I create my own middlewares? I mean what is this magic string "Cookies"? It's just from tutorials/examples...)
                LoginPath = new PathString("/base_site/login.aspx"),
                AccessDeniedPath = new PathString("/base_site/logon.aspx"), // Might want to create an error page...
                CookieName = "companyName_AccessLevel", //This is what he had in <forms name="companyName_AccessLevel">
                ReturnUrlParameter = "index.html", //What youd put in <forms defaultUrl="index.html">
                AutomaticAuthenticate = true,
                AutomaticChallenge = true

            });

            app.UseMvc();
        }

坦率地说,与我的同事根据他的 asp.net 经验提出的建议相比,这一切似乎非常复杂:

“只要把它放在你的 web.config 中”他告诉我

 <system.web>
    <authentication mode="Forms">
      <forms name="companyName_AccessLevel" loginUrl="/base_site/login.aspx" protection="All" timeout="60" defaultUrl="index.html"/>
    </authentication>
    <authorization>
      <deny users="?"/>
    </authorization>
  </system.web>

所以他当然不太热衷于和我一起研究这种“其他方式”。我可以以某种方式恢复到 web.config 吗?

【问题讨论】:

    标签: c# asp.net vb.net authentication asp.net-core


    【解决方案1】:

    Cookie 已加密,您必须使用类似的加密技术和共享密钥才能跨 2 个不同站点读取 Cookie。有一个 Nuget 包允许您配置旧的 asp.net 4.x 站点以与核心站点共享 cookie。这是它的官方文档。

    https://docs.asp.net/en/latest/security/data-protection/compatibility/cookie-sharing.html?highlight=share%20cookie#sharing-authentication-cookies-between-asp-net-4-x-and-asp-net-core-applications

    【讨论】:

      【解决方案2】:

      要配置跨应用程序的表单身份验证,您可以设置 Web.config 文件的表单和 machineKey 部分的属性 参与的所有应用程序的值相同 共享表单身份验证。

      https://msdn.microsoft.com/en-us/library/eb0zx8fc.aspx

      【讨论】:

        猜你喜欢
        • 2016-07-27
        • 2011-11-23
        • 2021-06-28
        • 2011-09-18
        • 2012-09-05
        • 2015-10-15
        • 2019-10-31
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多