发布后通过 IHttpContextAccessor 下载用户名时出错 ASP.NET
error downloading username by IHttpContextAccessor after publication ASP.NET
我创建供内部使用的应用程序,
正在尝试通过 IHttpContextAccessor 获取用户登录
控制器:
private Microsoft.AspNetCore.Http.IHttpContextAccessor _httpContextAccessor;
var userName = _httpContextAccessor.HttpContext.User.Identity.Name;
//var userName = @"ad\LOGINUSER";
如果我使用 _httpContextAccessor.HttpContext.User.Identity.Name 提取用户名; mam błąd na stronie
Error.
An error occurred while processing your request.
Request ID: |77762974-42ec74070b648c99.
Development Mode
Swapping to Development environment will display more detailed information about the error that occurred.
如果我使用 var userName = @"ad\LOGINUSER";一切正常
当然是发布后的错误,在电脑上正常编译一切正常
我使用的是.net core 3.1
我的启动代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using AppEcp.Models;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json.Serialization;
using Microsoft.AspNetCore.Server.IISIntegration;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.AspNetCore.Mvc.Infrastructure;
namespace AppEcp
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services
.AddMvc();
services
.AddHttpContextAccessor();
// services
// .TryAddSingleton<IActionContextAccessor, ActionContextAccessor>();
services
.AddAuthentication(IISDefaults.AuthenticationScheme);
services
.AddControllersWithViews()
.AddJsonOptions(options => options.JsonSerializerOptions.PropertyNamingPolicy = null);
// .AddJsonOptions(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver());
services.AddCors(c =>
{
c.AddPolicy("AllowOrigin", options => options.AllowAnyOrigin());
});
var connectionAd = @"Server=wsql3;Database=ActiveDirectory;Trusted_Connection=True;MultipleActiveResultSets=true";
services.AddDbContext<UzytkownicyDbContext>(options => options.UseSqlServer(connectionAd));
var connectionUrlop = @"Server=wsql3;Database=ECP;Trusted_Connection=True;MultipleActiveResultSets=true";
services.AddDbContext<EcpDbContext>(options => options.UseSqlServer(connectionUrlop));
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
// app.UseDatabaseErrorPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
// app.UseHsts();
}
// app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Ewidencja}/{id?}");
});
}
}
}
有谁知道我做错了什么吗?
您没有通过 UseAuthentication
启用 Authentication
:
app.UseAuthentication(); // add this line
app.UseAuthorization();
在 Controller 中,您不必通过 _httpContextAccessor.HttpContext....
访问 HttpContext,您可以通过 HttpContext.User
获取 User。
此外,您有责任检查用户是否 null :
var user = HttpContext.User;
if(user ==null)
{
...
}else{
var userName = user.Identity.Name;
...
}
最后,如果上述更改不能解决问题,能否请您向我们展示日志?
我创建供内部使用的应用程序,
正在尝试通过 IHttpContextAccessor 获取用户登录
控制器:
private Microsoft.AspNetCore.Http.IHttpContextAccessor _httpContextAccessor;
var userName = _httpContextAccessor.HttpContext.User.Identity.Name;
//var userName = @"ad\LOGINUSER";
如果我使用 _httpContextAccessor.HttpContext.User.Identity.Name 提取用户名; mam błąd na stronie
Error. An error occurred while processing your request. Request ID: |77762974-42ec74070b648c99.
Development Mode Swapping to Development environment will display more detailed information about the error that occurred.
如果我使用 var userName = @"ad\LOGINUSER";一切正常
当然是发布后的错误,在电脑上正常编译一切正常
我使用的是.net core 3.1
我的启动代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using AppEcp.Models;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json.Serialization;
using Microsoft.AspNetCore.Server.IISIntegration;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.AspNetCore.Mvc.Infrastructure;
namespace AppEcp
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services
.AddMvc();
services
.AddHttpContextAccessor();
// services
// .TryAddSingleton<IActionContextAccessor, ActionContextAccessor>();
services
.AddAuthentication(IISDefaults.AuthenticationScheme);
services
.AddControllersWithViews()
.AddJsonOptions(options => options.JsonSerializerOptions.PropertyNamingPolicy = null);
// .AddJsonOptions(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver());
services.AddCors(c =>
{
c.AddPolicy("AllowOrigin", options => options.AllowAnyOrigin());
});
var connectionAd = @"Server=wsql3;Database=ActiveDirectory;Trusted_Connection=True;MultipleActiveResultSets=true";
services.AddDbContext<UzytkownicyDbContext>(options => options.UseSqlServer(connectionAd));
var connectionUrlop = @"Server=wsql3;Database=ECP;Trusted_Connection=True;MultipleActiveResultSets=true";
services.AddDbContext<EcpDbContext>(options => options.UseSqlServer(connectionUrlop));
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
// app.UseDatabaseErrorPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
// app.UseHsts();
}
// app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Ewidencja}/{id?}");
});
}
}
}
有谁知道我做错了什么吗?
您没有通过
UseAuthentication
启用Authentication
:app.UseAuthentication(); // add this line app.UseAuthorization();
在 Controller 中,您不必通过
_httpContextAccessor.HttpContext....
访问 HttpContext,您可以通过HttpContext.User
获取 User。此外,您有责任检查用户是否 null :
var user = HttpContext.User; if(user ==null) { ... }else{ var userName = user.Identity.Name; ... }
最后,如果上述更改不能解决问题,能否请您向我们展示日志?