【发布时间】: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