如何将Winform应用程序中的cookie发送到服务器ASP.MVC 4

时间:2014-09-15 15:43:09

标签: asp.net-mvc winforms cookies mac-address

我有一个ASP.net MVC 4应用程序和一个winform应用程序。我的MVc需要通过Mac地址授权保护。我的winform将向他们发送mac地址,如: http://example.com/login/?mac=XX-XX-XX-XX-XX(使用查询字符串)。它完美运行,更安全。我想隐藏地址栏中的mac地址。我有一个想法,即在winform应用程序中创建cookie并将cookie发送到服务器。可能吗?给我建议,非常感谢你阅读我的问题。

1 个答案:

答案 0 :(得分:0)

我只是加密MAC地址并发送到服务器。使用共享密钥进行加密,该秘密仅为Web服务器所知才能解密。 IMO更加安全和可扩展。

更新

选项1:使用Cookie

是的,您可以使用Cookie发送信息,请参阅下面的代码段:

var baseAddress = new Uri("http://example.com");
var cookieContainer = new CookieContainer();
using (var handler = new HttpClientHandler() { CookieContainer = cookieContainer })
using (var client = new HttpClient(handler) { BaseAddress = baseAddress })
{
    var content = new FormUrlEncodedContent(new[]
    {
        new KeyValuePair<string, string>("foo", "bar"),
        new KeyValuePair<string, string>("baz", "bazinga"),
    });
    cookieContainer.Add(baseAddress, new Cookie("CookieName", "cookie_value"));
    var result = client.PostAsync("/test", content).Result;
    result.EnsureSuccessStatusCode();
}

从我获得代码示例的位置参考How do I set a cookie on HttpClient's HttpRequestMessage

请参阅此link,这可能对设置路径有用。

选项2:在POST请求中发送数据(首选选项)

   private static async Task RunAsync()
    {
        using (var client = new HttpClient())
        {
            client.BaseAddress = new Uri("http://example.com"); //Change the link
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            // HTTP GET
            HttpResponseMessage response = await client.GetAsync("api/values/1");
            if (response.IsSuccessStatusCode)
            {
                Product product = await response.Content.ReadAsAsync<Product>();
                Console.WriteLine("{0}\t${1}\t{2}", product.Name, product.Price, product.Category);
            }

            // HTTP POST
            var gizmo = new Product() {Name = "Gizmo", Price = 100, Category = "Widget"};
            response = await client.PostAsJsonAsync("api/values", gizmo);
            if (response.IsSuccessStatusCode)
            {
                Uri gizmoUrl = response.Headers.Location;

                // HTTP PUT
                gizmo.Price = 80; // Update price
                response = await client.PutAsJsonAsync(gizmoUrl, gizmo);

                // HTTP DELETE
                response = await client.DeleteAsync(gizmoUrl);
            }
        }
    }

有关更多信息,请参阅msdn article