示例#1
0
        public async Task <IActionResult> ServerCall(ApiProxyModel model)
        {
            try
            {
                HttpResponseMessage response = await Proxy.ServerCall(model, this.Token);

                return(Content(await response.Content.ReadAsStringAsync()));
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
示例#2
0
        public async Task <IActionResult> LoginUser(string returnUrl, string email, string contrasena)
        {
            var self      = this;
            var userModel = new LoginModel
            {
                Email      = email,
                Contrasena = contrasena
            };
            var model = new ApiProxyModel
            {
                HttpMethod = Models.HttpMethod.Post,
                EndPoint   = "/api/Token",
                UrlParams  = null,
                Body       = JsonConvert.SerializeObject(userModel)
            };

            try
            {
                HttpResponseMessage response = await Proxy.ServerCall(model, base.Token);

                var tokenjson = Content(await response.Content.ReadAsStringAsync());
                var token     = JObject.Parse(tokenjson.Content);

                var Issuer = self._config.GetValue <string>("Webapi");
                if ((string)token["token"] != null || (string)token["token"] != "")
                {
                    var claims = new List <Claim>
                    {
                        new Claim(ClaimTypes.Name, email, ClaimValueTypes.String, Issuer)
                    };
                    var userIdentity  = new ClaimsIdentity(claims, "Bearer");
                    var userPrincipal = new ClaimsPrincipal(userIdentity);

                    await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme,
                                                  userPrincipal,
                                                  new AuthenticationProperties
                    {
                        ExpiresUtc   = DateTime.UtcNow.AddMinutes(20),
                        IsPersistent = false,
                        AllowRefresh = false
                    });

                    base.Token = Convert.ToString(token["token"]);

                    return(GoToReturnUrl(returnUrl));
                }

                return(RedirectToAction(nameof(Denied)));
            }
            catch (Exception ex)
            {
                if (ex.Message == "Response status code does not indicate success: 401 (Unauthorized).")
                {
                    return(RedirectToAction(nameof(Denied)));
                }
                else
                {
                    throw ex;
                }
            }
        }