我正在使用Visual Studio 2012开发带有WPF(.NET 4.0)客户端的ASP WebAPI(ASP MVC 4)应用程序.客户端需要登录到服务器.我使用FormsAuthentication和身份验证cookie登录.登录已经在ASP MVC中正常工作.
问题是,尽管登录在服务器上成功执行并且cookie被发送回客户端,但是在后续调用服务器时不会发送cookie,即使它CookieContainer与auth cookie集重用.
这是代码的简化版本:
客户
public async Task<UserProfile> Login(string userName, string password, bool rememberMe)
{
using (var handler = new HttpClientHandler() { CookieContainer = this.cookieContainer })
using (var httpClient = new HttpClient(handler))
{
httpClient.BaseAddress = new Uri("http://localhost:50000/");
httpClient.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
var result = await httpClient.PostAsJsonAsync("api/auth/login", new
{
username = userName,
password = password,
rememberMe = rememberMe
});
result.EnsureSuccessStatusCode();
var userProfile = await result.Content.ReadAsAsync<UserProfile>();
if (userProfile == null)
throw new UnauthorizedAccessException();
return …Run Code Online (Sandbox Code Playgroud) 我有一个.net核心webapi项目设置为接受跨源请求,如下所示
public void ConfigureServices(IServiceCollection services)
{
services.AddCors();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHsts();
}
app.UseCors(opts => opts
.WithOrigins("https://fiddle.jshell.net")
.AllowCredentials()
.AllowAnyMethod()
.AllowAnyHeader());
app.UseHttpsRedirection();
app.UseMvc();
}
Run Code Online (Sandbox Code Playgroud)
它具有一个带有GET方法的值控制器,如下所示
[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
// GET api/values
[HttpGet]
public ActionResult<IEnumerable<string>> Get()
{
return Ok("cookies: " + string.Join(", ", HttpContext.Request.Cookies.Select(x => x.Key)));
}
}
Run Code Online (Sandbox Code Playgroud)
现在,我试图像这样从浏览器发送获取请求
public void ConfigureServices(IServiceCollection services)
{
services.AddCors();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if …Run Code Online (Sandbox Code Playgroud) 有人能告诉我如何在两个端口上同时运行Django吗?默认的Django配置仅侦听端口8000.我还想在端口xxxx上运行另一个实例.我想将所有请求重定向到第二个端口到我的Django应用程序中的特定应用程序.
我需要使用默认的Django安装完成此操作,而不是使用像nginx,Apache等网络服务器.
谢谢
假设我的Django应用程序中有两个应用程序.现在我不是指两个单独的Django应用程序,而是"app"目录中的单独文件夹.让我们把这个app1和app2
我希望端口8000上的app1所有请求都转到端口XXXX上的所有请求app2
HTH.
当我使用 Microsoft.Owin.StaticFiles 时,如何在将响应发送到客户端之前修改响应?
FileServerOptions options = new FileServerOptions();
options.FileSystem = new Microsoft.Owin.FileSystems.PhysicalFileSystem(Path.Combine(Environment.CurrentDirectory, "Content/"));
options.DefaultFilesOptions.DefaultFileNames = new string[] { "index.htm", "index.html" };
options.StaticFileOptions.OnPrepareResponse = (r) =>
{
r.OwinContext.Response.WriteAsync("test");
};
options.EnableDefaultFiles = true;
app.UseFileServer(options);
Run Code Online (Sandbox Code Playgroud)
“测试”永远不会写入响应。我尝试使用另一个中间件,直到执行 StaticFiles 中间件:
app.Use((ctx, next) =>
{
return next().ContinueWith(task =>
{
return ctx.Response.WriteAsync("Hello World!");
});
});
FileServerOptions options = new FileServerOptions();
options.FileSystem = new Microsoft.Owin.FileSystems.PhysicalFileSystem(Path.Combine(Environment.CurrentDirectory, "Content/"));
options.DefaultFilesOptions.DefaultFileNames = new string[] { "index.htm", "index.html" };
options.EnableDefaultFiles = true;
app.UseFileServer(options);
Run Code Online (Sandbox Code Playgroud)
但这没有用。如何修改响应?
我有两个弹簧启动应用程序.
我在application.properties文件中使用此属性设置了端口
server.port=${port:9090}
Run Code Online (Sandbox Code Playgroud)
两个模块都有/ login,/ signup,无需通过以下代码完成身份验证即可访问.
http.authorizeRequests()
.antMatchers("/signup", "/login").permitAll()
Run Code Online (Sandbox Code Playgroud)
任何其他请求都要求对用户进行身份验证.
如果我一次使用一个模块没有问题,
但是如果尝试在同一时间来回使用它们,那么问题是我每次使用另一个时都必须再次登录到之前的应用程序.例如.
我很确定这是因为模块2重置了jessionid.
HTTP cookie端口是否特定? 我已经阅读了这篇文章,其中指出cookie不是特定于端口的.
但必须有一个解决方案,这样我每次切换应用程序时都不必登录.
我有2台服务器。一个在localhost:5555上托管 next.js 应用程序,另一个在localhost:4444上托管 api 的快速服务器。
身份验证 api 返回一个 cookie,但是这不是在localhost:5555上运行的浏览器中设置的。
res.cookie('cokkieName', 'bob', {
domain: '127.0.0.1:5555',
maxAge: 900000,
httpOnly: true,
});
res.status(200).json({
session: jwtSigned,
role: 'default',
});
Run Code Online (Sandbox Code Playgroud)
我的 cors 设置是:
const options: cors.CorsOptions = {
allowedHeaders: ['Origin', 'X-Requested-With', 'Content-Type', 'Accept', 'X-Access-Token', 'Authorization'],
credentials: true,
methods: 'GET,HEAD,OPTIONS,PUT,PATCH,POST,DELETE',
origin: 'http://localhost:5555',
preflightContinue: false,
};
Run Code Online (Sandbox Code Playgroud)
我更喜欢使用 api 而不是通过 next.js 设置 cookie。
我试过交替 cors 设置但没有成功。客户端调用使用 axios 并且已经withCredentials设置。
我有一个小型的 Spring Boot 应用程序,它公开了一个带有两种方法“/upload”(POST)和“/show”(GET)的 REST 服务
当我在计算机上运行 Angular 2 应用程序时,http://computer.domain.com:4200,并在同一台计算机上的不同端口 http://computer.domain.com 上运行 Spring Boot Rest-service : 8080,角度 2 的 CSRF 部分不想发送带有标头的 XSRF 令牌。
如果我在 spring boot WAR 中部署 Angular GUI,以便可以通过http://computer.domain.com:8080访问 REST 和 GUI,那么与CSRF 相关的所有内容都可以工作。
我认为 Cookie 和 Angular 在从哪个端口发送时是“不可知的”,只要它源自同一服务器即可。
我是否缺少一些使角度发送 CSRF 标头的配置?
asp.net ×2
cookies ×2
javascript ×2
.net ×1
angular ×1
asp.net-core ×1
c# ×1
cors ×1
django ×1
jsessionid ×1
katana ×1
next.js ×1
node.js ×1
owin ×1
port ×1
python ×1
reactjs ×1
spring ×1
spring-boot ×1