相关疑难解决方法(0)

在 ASP.NET Core 5.0 Web API 中实现 DelegatingHandler?

    public class AuthenticationHandler : DelegatingHandler
    {
        protected override async Task<HttpResponseMessage> SendAsync(
            HttpRequestMessage req, CancellationToken cancellationToken)
        {
            Debug.WriteLine("Process request");
            // Call the inner handler.
            var response = await base.SendAsync(req, cancellationToken);
            Debug.WriteLine("Process response");
            return response;
        }
    }
Run Code Online (Sandbox Code Playgroud)

解决方案文件:https://i.stack.imgur.com/M4yv6.png

我能找到的唯一答案是旧版本的 Web API,其中解决方案的结构非常不同

c# asp.net asp.net-web-api asp.net-core

8
推荐指数
1
解决办法
2万
查看次数

ASP .NET Core webapi 在中间件中设置 cookie

我正在尝试在执行操作后设置 cookie,但正在努力使其正常工作。如果我从控制器而不是从中间件设置它,我设法看到了 cookie。我玩过配置的顺序,什么也没有。代码示例来自一个干净的webapi创建的项目,所以如果有人想玩它很简单,只需创建一个空的webapi,添加CookieSet类并将Startup类替换为下面的类(仅添加了cookie策略选项)

这是我的中间件

public class CookieSet
{
    private readonly RequestDelegate _next;

    public CookieSet(RequestDelegate next)
    {
        _next = next;
    }

    public async Task Invoke(HttpContext context)
    {
        await _next.Invoke(context);
        var cookieOptions = new CookieOptions()
        {
            Path = "/",
            Expires = DateTimeOffset.UtcNow.AddHours(1),
            IsEssential = true,
            HttpOnly = false,
            Secure = false,
        };
        context.Response.Cookies.Append("test", "cookie", cookieOptions);
    }
}
Run Code Online (Sandbox Code Playgroud)

我已经添加了 p 分配并检查了执行永远不会到达那里,在 Cookies.Append 行它停止执行,所以有什么事情发生了我无法弄清楚。

这是我的 Startup 课程

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; …
Run Code Online (Sandbox Code Playgroud)

cookies middleware asp.net-core

5
推荐指数
1
解决办法
5532
查看次数