我有1个“网络核心6”网络应用,使用“raz”网页。 我将前端改为“javascript UI”,并增加了该项目的控制人员,为Ajax电话提供服务。 管制人员要求管理人员采用正常的扶养注射(DI)的商业逻辑。
在现有的网页上,我打电话给一位使用, 采用DI。 它一直在工作,我看到能够有6个厨师。
If I call the same code in the manager class from the controllers using one of the Ajax calls - the cookies collection is empty. Furthermore, if I add the basic code below to add a cookie, as a test inline to the existing code, no cookie is visible after the code has processed (and with no errors).
_httpContextAccessor.HttpContext.Response.Cookies.Append("Test", "TestValue");
I am using the DI code below in my Startup.cs
services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();
and have tried adding
services.AddHttpContextAccessor();
并且我还试验了各种CookieOptions,但迄今没有uck。
是否有任何想法说明为什么守则在从控制者手中操作时不发挥作用,而是从网页上操作?
ADDED (per Guru request):
Here is the code in the manager:
public class AppManager : IAppManager
{
private readonly IHttpContextAccessor _httpContextAccessor;
public AppManager(IHttpContextAccessor httpContextAccessor )
{
_httpContextAccessor = httpContextAccessor;
}
public string ReadCookie(string key)
{
// ADDED THIS LINE BELOW - BUT IT DOES NOTHING
// STARTS WITH 0 COOKIES AND IS 0 COOKIES EVEN AFTER THE APPEND
_httpContextAccessor.HttpContext.Response.Cookies.Append("Test", "TestValue");
var data = _httpContextAccessor.HttpContext.Request.Cookies[key];
return data;
}
public void WriteCookie(string key, string value, int? daysToPersist = null)
{
// HAVE TRIED VARIOUS OPTIONS HERE
var options = new CookieOptions
{
SameSite = SameSiteMode.None,
Secure = true,
IsEssential = true,
HttpOnly = false
};
if (daysToPersist > 0)
options.Expires = DateTime.Now.AddDays((double)daysToPersist);
else
options.Expires = DateTime.Now.AddSeconds((double)60);
_httpContextAccessor.HttpContext.Response.Cookies.Append(key, value, options);
}
public void DeleteCookie(string key)
{
_httpContextAccessor.HttpContext.Response.Cookies.Delete(key);
}
}
Here is the code in the Controller:
public Guid GetUserGuidFromCookie()
{
string userGuidString = ReadCookie(UserGuidKey);
if (!Guid.TryParse(userGuidString, out Guid userGuid))
{
userGuid = Guid.NewGuid();
WriteCookie(UserGuidKey, userGuid.ToString(), 14);
}
return userGuid;
}
增加方案 7/28/2023
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
Addedstartup.cs 7/28/2023
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers()
.AddJsonOptions(options =>
{
options.JsonSerializerOptions.PropertyNamingPolicy = null;
}
);
services.AddRazorPages() //.AddRazorRuntimeCompilation()
.AddRazorOptions(options =>
{
options.PageViewLocationFormats.Add("/Pages/DisplayTemplates/{0}.cshtml");
});
services.AddSingleton<IAppSettings>(Configuration.GetSection("App").Get<AppSettings>());
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddScoped<IAppManager, AppManager>();
services.AddCors(o => o.AddPolicy("ApiPolicy", builder =>
{
builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader();
}));
services.AddHttpContextAccessor();
}
public void Configure( IApplicationBuilder app, IWebHostEnvironment env,
IDataRepo dataRepo)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseCors();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapRazorPages();
});
}
}