wly*_*les 11 c# asp.net asp.net-core
我有一个带有Razor Pages的ASP.NET Core 2.1 Web应用程序,它具有在appsettings.json文件中定义的AAD身份验证信息(由默认应用程序模板提供 - 请参阅下面我如何到达那里).但是,当尝试在配置中配置身份验证时Startup.cs,我没有任何配置值appsettings.json.如果我IConfiguration在调试器中检查对象,那么它似乎只有环境变量配置:
这是Startup.ConfigureServices问题所在的方法:
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddAuthentication(AzureADDefaults.AuthenticationScheme)
.AddAzureAD(options =>
{
// This is from the default template. It should work, but the relevant settings aren't there so options isn't populated.
this.Configuration.Bind("AzureAd", options);
// This of course works fine
options.Instance = "MyInstance";
options.Domain = "MyDomain";
options.TenantId = "MyTenantId";
options.ClientId = "MyClientId";
options.CallbackPath = "MyCallbackPath";
});
services.AddMvc(options =>
{
var policy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.Build();
options.Filters.Add(new AuthorizeFilter(policy));
})
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}
Run Code Online (Sandbox Code Playgroud)
并且服务配置在重要的情况下(请注意,这是在服务结构无状态服务之上构建的):
protected override IEnumerable<ServiceInstanceListener> CreateServiceInstanceListeners()
{
return new ServiceInstanceListener[]
{
new ServiceInstanceListener(serviceContext =>
new KestrelCommunicationListener(serviceContext, "ServiceEndpoint", (url, listener) =>
{
ServiceEventSource.Current.ServiceMessage(serviceContext, $"Starting Kestrel on {url}");
return new WebHostBuilder()
.UseKestrel(opt =>
{
int port = serviceContext.CodePackageActivationContext.GetEndpoint("ServiceEndpoint").Port;
opt.Listen(IPAddress.IPv6Any, port, listenOptions =>
{
listenOptions.UseHttps(GetCertificateFromStore());
listenOptions.NoDelay = true;
});
})
.ConfigureServices(
services => services
.AddSingleton<StatelessServiceContext>(serviceContext))
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>()
.UseServiceFabricIntegration(listener, ServiceFabricIntegrationOptions.None)
.UseUrls(url)
.Build();
}))
};
}
Run Code Online (Sandbox Code Playgroud)
为了创建这个服务,我使用了VS2017中的向导.我选择了一个现有的服务结构项目(.sfproj)并选择Services > Add > New Service Fabric Service并选择Stateless ASP.NET Core [for .NET Framework],然后在我选择的下一页Web Application(使用Razor Pages,而不是MVC)并点击Change Authentication我选择的位置Work or School Accounts并输入我的AAD信息.只是我对这个模板所做的更改都将里面调用的代码AddAzureAD中Startup.ConfigureServices,并设置appsettings.json文件总是被复制到输出目录.
为什么appsettings.json文件没有加载到配置中?据我了解,这应该是默认发生的,但似乎缺少某些东西......
Dav*_*idG 11
WebHostBuilderappsettings.json默认情况下不加载,您需要手动调用AddJsonFile.例如:
return new WebHostBuilder()
.UseKestrel(opt =>
{
//snip
})
.ConfigureAppConfiguration((builderContext, config) =>
{
config.AddJsonFile("appsettings.json", optional: false);
})
.ConfigureServices(
services => services
.AddSingleton<StatelessServiceContext>(serviceContext))
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>()
.UseServiceFabricIntegration(listener, ServiceFabricIntegrationOptions.None)
.UseUrls(url)
.Build();
Run Code Online (Sandbox Code Playgroud)
或者,您可以使用WebHost.CreateDefaultBuilder哪个将加载更多默认值.
另一种方法是通过ConfigurationBuilder然后使用该UseConfiguration方法手动创建配置。
var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", false, true)
.Build();
var host = new WebHostBuilder()
.UseConfiguration(configuration)
.UseKestrel()
.UseStartup<Startup>();
Run Code Online (Sandbox Code Playgroud)
主要目的是在实施时提供一点灵活性,但他们经常犯“少即是多”的错误。你必须明确地说出你想要什么,这样管道就保持相对较小。
| 归档时间: |
|
| 查看次数: |
3881 次 |
| 最近记录: |