Web api在客户端身份验证期间获取令牌

时间:2018-08-08 03:05:44

标签: c# authentication asp.net-web-api2 token

我想将数据发送到我的API,并将计算出的元素作为返回值,并将生成的令牌作为与计算出的元素绑定为键,我的问题是,在客户端身份验证期间,我还不知道如何恢复令牌。我的api。

这是我的API代码:

public class PricingController : ApiController
{       
    [BasicAuthentication]
    public async Task<HttpResponseMessage> GetPricing(ArrayList paramList)
    {
        if (paramList.Count > 0)
        {
            try
            {
                Double Premium = 0;


                Profil pf = Newtonsoft.Json.JsonConvert.DeserializeObject<Profil>(paramList[0].ToString());

                //////////////////Charger le programme etr lancer le calcul
                //var prog = db.Profils.Find(pf.IdProfil);

                if (pf.IdProfil == 1)
                {
                    CourtierImmobilier Ci = new CourtierImmobilier(paramList);
                    List<string> verify = Ci.VerifObject();
                    if (verify[0] == "False")
                    {
                        Premium=Ci.calculate();
                        return Request.CreateResponse(HttpStatusCode.OK, new { premium = Premium, IdSubmession = "a0000123", DateExpire = DateTime.Today });
                    }
                    else
                    {
                        var message = verify[1];
                        HttpError err = new HttpError(message);
                        return Request.CreateErrorResponse(HttpStatusCode.BadRequest, err);
                    }
                }
                else
                {
                    var message = string.Format("Profil Introuvable!!!");
                    HttpError err = new HttpError(message);
                    return Request.CreateErrorResponse(HttpStatusCode.NotFound, err);
                }                    

            }
            catch (Exception e)
            {
                string m = e.Message;
                return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, m );
            }
        }
        else
        {
            var message = string.Format("Liste d'objets est vide !!!");
            HttpError err = new HttpError(message);
            return Request.CreateErrorResponse(HttpStatusCode.BadRequest, err);
        }
    }
}

这是我的客户代码:

[HttpPost]
    public async Task<ActionResult> Index(bool b = false)
    {

        HttpClient client = new HttpClient();
        string authInfo = "xxxX" + ":" + "fduujsj";
        authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", authInfo);

        Uri baseAddress = new Uri("http://localhost:44235/");
        client.BaseAddress = baseAddress;

        JArray paramList = new JArray();
        Client Cl = new Client { IdClient = "t018794", FirstName = "ALain", LastName = "Carpentier", Adress = "App 302, 789 fay", PostalCode = "H8y1H2", City = "souf", DriverLicense = "4389954587", BirthDate = Convert.ToDateTime("11/23/1984")};
        paramList.Add(JsonConvert.SerializeObject(CL));

        HttpResponseMessage response = client.PostAsJsonAsync("api/Pricing/Pricing", paramList).Result;

        try
        {
            if (response.IsSuccessStatusCode)
            {
                var list1 = await response.Content.ReadAsStringAsync();
                JavaScriptSerializer jsonSerializer = new JavaScriptSerializer();
                dynamic dobj = jsonSerializer.Deserialize<dynamic>(list1);
                var premium = dobj["premium"];
                var IdSubmession = dobj["IdSubmession"];
                //var DateExpire = dobj["DateExpire"];
                ViewBag.Premium = "Final Price : " + premium + "  ID : " + token;
                return View();

            }
            else
            {
                var ErrMsg = JsonConvert.DeserializeObject<dynamic>(response.Content.ReadAsStringAsync().Result);
                ViewBag.error = "  Error : " + (int)response.StatusCode + response.ReasonPhrase  + ErrMsg;
                return View();
            }
        }
        catch (Exception e)
        {
            string m = e.Message;
            return View();
        }
    }
}

0 个答案:

没有答案