【发布时间】:2016-09-28 17:42:13
【问题描述】:
我在 .NET Core 中自托管我的 Web API 时遇到问题。它在 IIS 上运行良好,但在尝试自托管时出现身份验证错误。我尝试在其他解决方案中使用 cookie 身份验证,但徒劳无功。我现在只有 1 条路线,但是在放置多个断点后,我注意到它甚至没有到达我的控制器的构造函数。如果有人能给我一个解决方案的提示,我将不胜感激:)
这是我的代码的 sn-ps。
Startup.cs
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(this.Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
app.UseApplicationInsightsRequestTelemetry();
app.UseApplicationInsightsExceptionTelemetry();
app.UseMvc();
}
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddApplicationInsightsTelemetry(this.Configuration);
// Add the database used
services.AddDbContext<VaultContext>(options => options.UseSqlServer(this.Configuration.GetConnectionString("VaultDatabase")));
// Add our repository type
services.AddScoped<VaultRepository, VaultRepository>();
services.AddScoped<UserResolverService, UserResolverService>();
services.AddScoped<PersonalizationServiceClient, PersonalizationServiceClient>();
services.AddScoped<PersistenceToDataModelConverter, PersistenceToDataModelConverter>();
services.AddMvc(config =>
{
var policy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.Build();
config.Filters.Add(new AuthorizeFilter(policy));
});
}
''services.AddMvc'' 中的配置 lamda 是我发现的唯一有效的方法,它迫使调用者提供他的 Windows 凭据,我稍后将根据他的配置文件分析和显示信息。如果这不是正确的方法,请告诉我。
还有我的控制器类
public VaultController(VaultRepository repository, UserResolverService currentuser, PersonalizationServiceClient personalizationService, PersistenceToDataModelConverter persistenceToDataModelConverter)
{
this.repository = repository;
this.currentuser = currentuser;
this.personalizationService = personalizationService;
this.persistenceToDataModelConverter = persistenceToDataModelConverter;
}
/// <summary>
/// The get profile.
/// </summary>
/// <returns>
/// The <see cref="IActionResult"/>.
/// </returns>
[HttpGet]
[Route("/Profile")]
[Produces(typeof(UserProfile))]
public IActionResult SearchProfile()
{
try
{
if (!this.currentuser.IsAuthenticated)
{
throw new Exception("This service does not support anonymous calls.");
}
var profile = Task.Run(() => this.personalizationService.GetUserProfileAsync(this.currentuser.GetCurrentWindowsIdentityName)).Result;
var userProfile = this.persistenceToDataModelConverter.Convert(profile);
userProfile.UserAdLogin = this.currentuser.GetCurrentWindowsIdentityName;
return this.Ok(userProfile);
}
catch (Exception ex)
{
return this.NotFound(ex);
}
}
这是我得到的错误
【问题讨论】:
标签: c# asp.net .net api asp.net-core