ProfileBase、HttpContext.Request.AnonymousId作为ASP.Net Core 2.



我目前正在尝试将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调用提出任何建议,那就太好了。

提前谢谢。

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

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

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

最新更新