public IActionResult Index() { IAnonymousIdFeature feature = HttpContext.Features.Get <IAnonymousIdFeature>(); if (feature != null) { string anonymousId = feature.AnonymousId; } return(View()); }
public void Configure(IApplicationBuilder app) { app.UseAnonymousId(new AnonymousIdCookieOptionsBuilder() .SetCustomCookieName("Anoymous_Cookie_Tracker") //.SetCustomCookieRequireSsl(true) //Uncomment this in the case of usign SSL, such as the default setup of .NET Core 2.1 .SetCustomCookieTimeout(120) ); app.Run(async context => { IAnonymousIdFeature feature = context.Features.Get <IAnonymousIdFeature>(); string anonymousId = feature.AnonymousId; await context.Response.WriteAsync($"Hello world with anonymous id {anonymousId}"); }); }
public async Task <IViewComponentResult> InvokeAsync() { string routeName = ""; string routeIdentifier = ""; var user = await userManager.GetUserAsync(UserClaimsPrincipal); if (user == null || string.IsNullOrEmpty(user.Name)) { IAnonymousIdFeature feature = HttpContext.Features.Get <IAnonymousIdFeature>(); if (feature != null) { routeIdentifier = feature.AnonymousId; } } else { routeIdentifier = user.CustomerId.ToString(); } int count = 0; try { // var IpAddress = contextAccessor.HttpContext.Connection.RemoteIpAddress.ToString(); var response = await this.restClient.Get <GetShoppingCartCountResponse>( routingConfig.ShoppingCartService.URL, routingConfig.ShoppingCartService.ProductCount, routeIdentifier, HeaderAccept.Json); count = response.Count; } catch (Exception e) { } return(View(count)); }