新足迹

 找回密码
 注册

精华好帖回顾

· 2016日本四国圆梦,仓敷姬路城崎奈良岚山京都东京 (全文完) (2016-4-29) violinlearner · 参加活动—Master Dessert:Fruit Tart 水果塔 (2012-8-11) lanshan
· 手把手教你如何出口 (2013-5-14) wspecific · 【三刀厨侠争霸赛】买菜便宜的好处 (2008-9-29) big_beast
Advertisement
Advertisement
查看: 1525|回复: 6

C#CLIENT 端当 做GET 的时候怎么传用户名密码到ASP.NET WEBAPI [复制链接]

发表于 2013-4-9 22:03 |显示全部楼层
此文章由 yangwulong1978 原创或转贴,不代表本站立场和观点,版权归 oursteps.com.au 和作者 yangwulong1978 所有!转贴必须注明作者、出处和本声明,并保持内容完整


1. for example I have a asp.net webapi : http://localhost/api/product.

2. I have a C# client to consume this webapi. something like that to get whole list of product.

// List all products.
HttpResponseMessage response = client.GetAsync("api/products").Result;  // Blocking call!
if (response.IsSuccessStatusCode)
{
    // Parse the response body. Blocking!
    var products = response.Content.ReadAsAsync<IEnumerable<Product>>().Result;
    foreach (var p in products)
    {
        Console.WriteLine("{0}\t{1};\t{2}", p.Name, p.Price, p.Category);
    }
}
else
{
    Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
}
我的问题是怎么从C#client 端传用户名,密码到WEB API 里去。
我想的是当C#CLIENT去 SERVER 的WEBAPI拿整个PRODUCTLIST前,先传用户名,密码过去,SERVER的WEB API验证,,如果是授权的用户,就让GET 如果不是,就不让GET 。。

POST 我会传用户名,密码过去,GET 我还不知道怎么传。
Advertisement
Advertisement

发表于 2013-4-9 22:21 |显示全部楼层
此文章由 dotnet 原创或转贴,不代表本站立场和观点,版权归 oursteps.com.au 和作者 dotnet 所有!转贴必须注明作者、出处和本声明,并保持内容完整
client:

  private HttpClient InitClient()
        {
            var client = new HttpClient { BaseAddress = new Uri("...") };
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            client.DefaultRequestHeaders.Authorization = new BasicAuthenticationHeaderValue("username", "password");
            return client;
        }

            var client = InitClient();
            var cartItem = new {
                };


            var response = client.PostAsJsonAsync("api/cartitem/post", cartItem).Result;


====
server:

using WebApiContrib.MessageHandlers;


    public class AuthenticationHandler : BasicAuthenticationHandler
    {
        protected override bool Authorize(string username, string password)
        {
            if (this.Validate(username, password))
            {
                var user = new User { Username = username };
                this.SetPrincipal(new GenericPrincipal(new APIIdentity(user), new string[] { }));
                return true;
            }
            return false;
        }

评分

参与人数 1积分 +3 收起 理由
yangwulong1978 + 3 感谢分享

查看全部评分

发表于 2013-4-9 22:45 |显示全部楼层
此文章由 yangwulong1978 原创或转贴,不代表本站立场和观点,版权归 oursteps.com.au 和作者 yangwulong1978 所有!转贴必须注明作者、出处和本声明,并保持内容完整
dotnet 发表于 2013-4-9 21:21
client:

  private HttpClient InitClient()

上面的    new BasicAuthenticationHeaderValue("username", "password");

是从  Thinktecture.IdentityModel.Http 里来的?

发表于 2013-4-9 22:52 |显示全部楼层
此文章由 dotnet 原创或转贴,不代表本站立场和观点,版权归 oursteps.com.au 和作者 dotnet 所有!转贴必须注明作者、出处和本声明,并保持内容完整
yangwulong1978 发表于 2013-4-9 21:45
上面的    new BasicAuthenticationHeaderValue("username", "password");

是从  Thinktecture.Identity ...

    public class BasicAuthenticationHeaderValue : AuthenticationHeaderValue
    {
        public BasicAuthenticationHeaderValue(string userName, string password)
            : base("Basic", EncodeCredential(userName, password))
        { }

        private static string EncodeCredential(string userName, string password)
        {
            Encoding encoding = Encoding.GetEncoding("iso-8859-1");
            string credential = String.Format("{0}:{1}", userName, password);

            return Convert.ToBase64String(encoding.GetBytes(credential));
        }
    }


you can inspect http header using Fiddler

发表于 2013-4-10 11:36 |显示全部楼层
此文章由 yangwulong1978 原创或转贴,不代表本站立场和观点,版权归 oursteps.com.au 和作者 yangwulong1978 所有!转贴必须注明作者、出处和本声明,并保持内容完整
dotnet 发表于 2013-4-9 21:52
public class BasicAuthenticationHeaderValue : AuthenticationHeaderValue
    {
        public B ...

今天我又看了一下,这是POST event, 这个我会,如果是GET呢,client.GetAsync("api/products").Result;
怎么传呢?

发表于 2013-4-10 17:21 |显示全部楼层
此文章由 yangwulong1978 原创或转贴,不代表本站立场和观点,版权归 oursteps.com.au 和作者 yangwulong1978 所有!转贴必须注明作者、出处和本声明,并保持内容完整
除了client.GetAsync("api/products/{0} /{1}", username, password).Result;
这种GET的时候通过URL传参数,,你们做GET的时候,还有什么办法传用户名,密码过去?

不是POST, 是GET()
Advertisement
Advertisement

发表于 2013-4-10 20:49 |显示全部楼层
此文章由 dotnet 原创或转贴,不代表本站立场和观点,版权归 oursteps.com.au 和作者 dotnet 所有!转贴必须注明作者、出处和本声明,并保持内容完整
yangwulong1978 发表于 2013-4-10 16:21
除了client.GetAsync("api/products/{0} /{1}", username, password).Result;
这种GET的时候通过URL传 ...

use authorization header  to pass username/password, don't use url string or form key/value pair for user credentials

发表回复

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Advertisement
Advertisement
返回顶部