Asp.Net核心WebAPI在未经授权的情况下重定向
我正在使用asp.net内核。我需要重定向对我的web api上未经授权的请求的响应。我找到了一些解决方案并尝试实施,但仍然不起作用。有人能帮我吗 这是我的密码:Asp.Net核心WebAPI在未经授权的情况下重定向,asp.net,asp.net-web-api,response.redirect,Asp.net,Asp.net Web Api,Response.redirect,我正在使用asp.net内核。我需要重定向对我的web api上未经授权的请求的响应。我找到了一些解决方案并尝试实施,但仍然不起作用。有人能帮我吗 这是我的密码: public partial class Startup { public Startup(IHostingEnvironment env) { var builder = new ConfigurationBuilder() .SetB
public partial class Startup
{
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
}
public IConfigurationRoot Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services
.AddMvc(options =>
{
options.Filters.Add(new RequireHttpsAttribute());
})
.AddJsonOptions(options =>
{
options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
});
services.Configure<IdentityOptions>(options =>
{
options.Cookies.ApplicationCookie.LoginPath = new PathString("/Account/Login");
options.Cookies.ApplicationCookie.Events = new CookieAuthenticationEvents()
{
OnRedirectToLogin = ctx =>
{
if ( (ctx.Request.Path.StartsWithSegments("/api") || ctx.Request.Path.Value.Contains("Account/Login") && ctx.Response.StatusCode == 200) )
{
ctx.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
return Task.FromResult<object>(null);
}
else
{
ctx.Response.Redirect(ctx.RedirectUri);
return Task.FromResult<object>(null);
}
}
};
});
//Add DI and other services
SetServices(services);
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
app.UseDeveloperExceptionPage();
CookieAuthenticationOptions options = new CookieAuthenticationOptions();
options.AuthenticationScheme = "Cookies";
options.CookieName = "GUW Cookie";
options.AutomaticAuthenticate = true;
options.AutomaticChallenge = true;
options.LoginPath = new PathString("/Account/Login");
app.UseCookieAuthentication(options);
app.UseMvc();
}
}
公共部分类启动
{
公共启动(IHostingEnvironment环境)
{
var builder=new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile(“appsettings.json”,可选:true,重载更改:true)
.AddJsonFile($“appsettings.{env.EnvironmentName}.json”,可选:true)
.AddenEnvironmentVariables();
Configuration=builder.Build();
}
公共IConfigurationRoot配置{get;}
//此方法由运行时调用。请使用此方法将服务添加到容器中。
public void配置服务(IServiceCollection服务)
{
//添加框架服务。
服务
.AddMvc(选项=>
{
options.Filters.Add(新的requireHttpAttribute());
})
.AddJsonOptions(选项=>
{
options.SerializerSettings.ContractResolver=新的CamelCasePropertyNamesContractResolver();
});
配置(选项=>
{
options.Cookies.applicationcokie.LoginPath=新路径字符串(“/Account/Login”);
options.Cookies.applicationcokie.Events=新建CookieAuthenticationEvents()
{
OnRedirectToLogin=ctx=>
{
if((ctx.Request.Path.StartsWithSegments(“/api”)| | ctx.Request.Path.Value.Contains(“帐户/登录”)&&ctx.Response.StatusCode==200))
{
ctx.Response.StatusCode=(int)HttpStatusCode.Unauthorized;
返回Task.FromResult(空);
}
其他的
{
响应重定向(ctx.RedirectUri);
返回Task.FromResult(空);
}
}
};
});
//添加DI和其他服务
设置服务(服务);
}
//此方法由运行时调用。请使用此方法配置HTTP请求管道。
公共void配置(IApplicationBuilder应用程序、IHostingEnvironment环境、iLogger工厂)
{
loggerFactory.AddConsole(Configuration.GetSection(“Logging”);
loggerFactory.AddDebug();
app.UseDeveloperExceptionPage();
CookieAuthenticationOptions=新建CookieAuthenticationOptions();
options.AuthenticationScheme=“Cookies”;
options.CookieName=“GUW Cookie”;
options.AutomaticAuthenticate=true;
options.AutomaticChallenge=true;
options.LoginPath=新路径字符串(“/Account/Login”);
app.UseCookieAuthentication(选项);
app.UseMvc();
}
}
以及两种安装控制器的方法
[HttpGet]
[Route("test")]
public string Test()
{
return "authorized";
}
[HttpGet]
[Route("login")]
[AllowAnonymous]
public async Task<IActionResult> Login()
{
//logs in the user
}
[HttpGet]
[路线(“测试”)]
公共字符串测试()
{
返回“已授权”;
}
[HttpGet]
[路线(“登录”)]
[异名]
公共异步任务登录()
{
//登录用户
}
这是因为我在Startup.cs的COnfigure方法中没有使用app.UseIdentity()吗
我没有使用EF的标识,我没有在任何地方使用EF
然后我试了一下:
services.Configure<CookieAuthenticationOptions>(options =>
{
options.Events = new CookieAuthenticationEvents()
{
OnRedirectToLogin = ctx =>
{
if ( ctx.Request.Path.StartsWithSegments("/api") && ctx.Response.StatusCode == 200)
{
ctx.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
return Task.FromResult<object>(null);
}
else
{
ctx.Response.Redirect(ctx.RedirectUri);
return Task.FromResult<object>(null);
}
}
};
});
services.Configure(选项=>
{
options.Events=新建CookieAuthenticationEvents()
{
OnRedirectToLogin=ctx=>
{
if(ctx.Request.Path.StartsWithSegments(“/api”)&&ctx.Response.StatusCode==200)
{
ctx.Response.StatusCode=(int)HttpStatusCode.Unauthorized;
返回Task.FromResult(空);
}
其他的
{
响应重定向(ctx.RedirectUri);
返回Task.FromResult(空);
}
}
};
});
没有效果
thnx