【问题标题】:Are ProfileBase, HttpContext.Request.AnonymousId available as a part of the ASP.Net Core 2.x suite? If so how do I access them.ProfileBase、HttpContext.Request.AnonymousId 是否作为 ASP.Net Core 2.x 套件的一部分提供?如果是这样,我该如何访问它们。
【发布时间】:2019-04-13 12:51:34
【问题描述】:

我目前正在尝试将 ASP.Net MVC API 4.6.2 应用程序迁移到 ASP.Net Core 2.1。

     private static string GetSessionId()
    {
        // Get from the session state, most reliable, but not likely available
        if (HttpContext.Current.Session != null && !string.IsNullOrEmpty(HttpContext.Current.Session.SessionID))
        {
            return HttpContext.Current.Session.SessionID;
        }

        // if the application has enabled anonymous tracking, then this is also reliable
        // (note: this requires <anonymousIdentification enabled="true" /> in the web.onfig.
        if (HttpContext.Current.Profile.IsAnonymous &&
            !string.IsNullOrEmpty(HttpContext.Current.Request.AnonymousID))
        {
            return HttpContext.Current.Request.AnonymousID;
        }

        // last resort, we track this ourselves.
        var telemetryId = HttpContext.Current.Request.Cookies[TelemetryKey] != null
            ? HttpContext.Current.Request.Cookies[TelemetryKey].Value
            : Guid.NewGuid().ToString();
        var telemetryCookie = new HttpCookie(TelemetryKey, telemetryId) { Expires = DateTime.Now.AddYears(1) };
        HttpContext.Current.Response.SetCookie(telemetryCookie);

        return telemetryId;
    }

现在在 .Net Core 版本中,我找不到等价物。是的,我已经通过 ConfigureServices() 注册了 IHttpContextAccessor 并且能够在这个服务类中访问它。但是,当前的 HttpContext 似乎没有 Sytem.Web.Http 命名空间提供的所有属性。

是否有其他方法可以检查用户是否匿名?

    (HttpContext.Current.Profile.IsAnonymous && 
    string.IsNullOrEmpty(HttpContext.Current.Request.AnonymousID))

此外,如果能就在 .Net 标准类库中保留 Http 调用提出任何建议,那就太好了。

提前致谢。

【问题讨论】:

    标签: .net asp.net-core migration httpcontext asp.net-core-2.1


    【解决方案1】:

    发现 AnonymousId 已在 .Net Core 中停止使用,因为它的使用可忽略不计,如有必要,需要通过自定义中间件实现。 在编写实现相同目标的自定义中间件时,我发现有人之前已经走过这条路并编写了一个自定义中间件,可以在以下链接中找到:https://github.com/aleripe/AnonymousId

    它节省了我很多时间,如果您正在寻找这类东西,希望它也能帮到您。

    仅供参考:我在 .Net Core 中找不到太多关于 ProfileBase 库的信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-30
      • 2020-12-19
      • 1970-01-01
      • 2018-07-07
      • 2014-01-29
      • 1970-01-01
      相关资源
      最近更新 更多