【问题标题】:Pass header cookies from Web API post to header location page将 Web API 帖子中的标头 cookie 传递到标头位置页面
【发布时间】:2018-01-04 05:02:16
【问题描述】:

我有一个从外部源发布到的 ASP.Net Web API。发布到 web api 的值用于确定用户在我们网站上的权利。所以 web api 然后将我的业务逻辑的结果对象作为一堆 cookie 传递给我们的 asp 登录页面。问题是 cookie 在 web api 将响应路由到的网页中不再可用。

这里是web api:

    [HttpPost]
    public HttpResponseMessage Reports(ReportRequest reportRequest)
    {
        if (reportRequest != null)
        {
            var reportAccess = new SwitchBL().CheckUserAccess(reportRequest);

            var response = Request.CreateResponse(HttpStatusCode.Moved);
            response.Headers.Location = new Uri(BaseUrl() + "/menu.aspx");

            var json = JsonConvert.SerializeObject(reportAccess);
            Dictionary<string, string> biscuitTin = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);

            foreach (var biscuit in biscuitTin)
            {
                var cookie =
                    new CookieHeaderValue(biscuit.Key, biscuit.Value ?? "")
                    {
                        Expires = DateTimeOffset.Now.AddDays(1),
                        Domain = Request.RequestUri.Host == "localhost" ? null : Request.RequestUri.Host,
                        HttpOnly = true
                    };
                //cookierJar.Add(cookie);
                response.Headers.AddCookies(new CookieHeaderValue[] {cookie} );
            }
            return response;
        }
        return Request.CreateResponse(HttpStatusCode.BadRequest);
    }

到目前为止,我非常简单的 aspx 页面总是显示 count = 0:

public partial class menu : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        var cookieCount = Request.Cookies.Count;
    }
}

web api 和 aspx 页面位于同一个项目中,因此托管在一个站点中。我不想使用会话变量,也不想在查询字符串中传递值。是否有另一种方法可以将数据从 web api 传递到路由页面,或者我在这里遗漏了什么?

顺便说一句,如果我使用 Postman 发布到 api,cookie 会在 web api 的响应标头中可见,因此会创建 cookie。如果我使用另一个网页发布,使用 Fiddler,我可以在 api 的响应中看到 cookie,但是(接收)asp 页面中没有 cookie。

更新 感谢Kai的回答,我现在可以在我的路由asp页面中获取在response.Headers.Location中设置的cookie。我在该页面中有一个断点,所以我知道它正在被命中,并且 cookie 计数现在符合预期。但是,浏览器不会呈现路由页面。它保留在原始发布页面上。这是我在我的帖子模拟器页面中用来调用 web api 的代码:

protected void Page_Load(object sender, EventArgs e)
{

}

protected async void DoIt_OnClick(object sender, EventArgs e)
{
    var reportRequest = new ReportRequest();
    reportRequest.EmailAddress = Email.Text;
    reportRequest.UserNumber = UserCode.Text;
    reportRequest.MobileNumber = MobileNumber.Text;
    reportRequest.Password = Password.Text;
    reportRequest.Country = Country.Text;
    reportRequest.AccountNumber = AccountNumber.Text;
    reportRequest.AccountType = AccountType.Text;
    reportRequest.ReportType = ReportType.Text == "" ? 0 : Convert.ToInt32(ReportType.Text);
    reportRequest.PhoneInfo = PhoneInfo.Text;

    await GoThereAsync(reportRequest);

}

public async Task<Uri> GoThereAsync(ReportRequest reportRequest)
{
    using (var client = new HttpClient())
    {
        client.BaseAddress = new Uri("http://localhost:7789/");
        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/plain"));

        var response = await client.PostAsJsonAsync<ReportRequest>("api/switch/reports", reportRequest);

        if (response.IsSuccessStatusCode)
        {
            return response.RequestMessage.RequestUri;
        }
        return null;
    }
}

总结一下:Emulator.aspx 会 POST 到 web api。 Web API 将 cookie 和位置设置为 home.aspx。 Home.aspx 接收 cookie(调试步骤进入代码隐藏),但浏览器仍保留在 Emulat.aspx 上并且不呈现 home.aspx。

【问题讨论】:

    标签: c# cookies asp.net-web-api


    【解决方案1】:

    试试CookieHeaderValue.Path = "/"

    来自Microsoft Docs

    路径:将 cookie 限制为域内的指定路径。如果未指定,则使用请求 URI 的路径。

    【讨论】:

    • 我有路径,并且在我所有的摆弄中都删除了它。按照建议放回去修复了 cookie 问题。请查看我的问题第二部分的更新。
    • redirect:response.Headers.Location = new Uri("http://localhost:7789/");
    • 在 post 操作中已经有了:response.Headers.Location = new Uri(BaseUrl() + "/menu.aspx");
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-20
    • 2012-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多