Asp.net core 如何配置ASP.NET核心应用程序以使用windows身份验证?
我想将ASP.NET应用程序配置为根据环境使用不同的身份验证。所以对于开发环境,我希望使用Windows身份验证,对于所有其他环境,我希望使用Facebook身份验证 我已经为非开发环境配置了Facebook身份验证。如何为开发环境配置windows身份验证?Windows身份验证只在开发过程中使用,因此开发人员不必在VS中每次运行应用程序时都登录。我们有多个开发人员,这意味着Windows身份将因执行者而异。创建windows标识后,我将向windows标识添加声明Asp.net core 如何配置ASP.NET核心应用程序以使用windows身份验证?,asp.net-core,.net-core,coreclr,Asp.net Core,.net Core,Coreclr,我想将ASP.NET应用程序配置为根据环境使用不同的身份验证。所以对于开发环境,我希望使用Windows身份验证,对于所有其他环境,我希望使用Facebook身份验证 我已经为非开发环境配置了Facebook身份验证。如何为开发环境配置windows身份验证?Windows身份验证只在开发过程中使用,因此开发人员不必在VS中每次运行应用程序时都登录。我们有多个开发人员,这意味着Windows身份将因执行者而异。创建windows标识后,我将向windows标识添加声明 public class
public class Startup
{
public Startup(IHostingEnvironment env)
{
// some stuff here for building configuration
}
public IConfigurationRoot Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthorization();
services.AddMvc(config =>
{
var policy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.Build();
config.Filters.Add(new AuthorizeFilter(policy));
});
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IApplicationLifetime appLifetime)
{
if(env.IsDevelopment())
{
// How do i use windows authentication here
}
else
{
// this is my custom extension method
app.UseFacebookAuthentication();
}
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
}
Windows身份验证是在program.cs中的web主机配置期间配置的
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build();
var host=new WebHostBuilder()
.UseKestrel()
.UseContentRoot(目录.GetCurrentDirectory())
.Useii整合()
.UseStartup()
.Build();
特别是useisintegration()行
现在它自己什么都不做,还需要在aspNetCore节点的web.config中进行配置
<aspNetCore
processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%"
stdoutLogEnabled="false"
stdoutLogFile=".\logs\stdout"
forwardWindowsAuthToken="true" />
需要设置forwardWindowsAuthToken
值
因此,不能在env.IsDevelopment()检查中执行此操作。Windows Auth是在program.cs中的web主机配置期间配置的
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build();
var host=new WebHostBuilder()
.UseKestrel()
.UseContentRoot(目录.GetCurrentDirectory())
.Useii整合()
.UseStartup()
.Build();
特别是useisintegration()行
现在它自己什么都不做,还需要在aspNetCore节点的web.config中进行配置
<aspNetCore
processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%"
stdoutLogEnabled="false"
stdoutLogFile=".\logs\stdout"
forwardWindowsAuthToken="true" />
需要设置forwardWindowsAuthToken
值
因此,不能在env.IsDevelopment()检查中执行此操作。如果使用IIS Express进行开发环境,有一种快速设置Windows身份验证的方法。属性文件夹中有一个
launchSettings.json
,您可以轻松修改它以使用Windows身份验证进行开发,而无需修改类启动
在此文件中,您可以将“windowsAuthentication”更改为true,“anonymousAuthentication”更改为false
以下是示例launchSettings.json
:
{
"iisSettings": {
"windowsAuthentication": true,
"anonymousAuthentication": false,
"iisExpress": {
"applicationUrl": "http://localhost:6366/",
"sslPort": 0
}
},
profiles": {
"IIS Express": {...
}
修改后,您可以选择IIS Express作为调试目标来运行应用程序。如果您将IIS Express用于开发环境,则有一种快速设置Windows身份验证的方法。属性文件夹中有一个
launchSettings.json
,您可以轻松修改它以使用Windows身份验证进行开发,而无需修改类启动
在此文件中,您可以将“windowsAuthentication”更改为true,“anonymousAuthentication”更改为false
以下是示例launchSettings.json
:
{
"iisSettings": {
"windowsAuthentication": true,
"anonymousAuthentication": false,
"iisExpress": {
"applicationUrl": "http://localhost:6366/",
"sslPort": 0
}
},
profiles": {
"IIS Express": {...
}
修改后,可以通过选择IIS Express作为调试目标来运行应用程序