Beispiel #1
0
        public async Task <ActionResult> doLogin([FromBody] LoginRequestParam model)
        {
            Dictionary <string, string> dict = new Dictionary <string, string>();

            dict["client_id"]     = model.ClientId;
            dict["client_secret"] = configuration[$"IdentityClients:{model.ClientId}:ClientSecret"];
            dict["grant_type"]    = configuration[$"IdentityClients:{model.ClientId}:GrantType"];
            dict["username"]      = model.UserName;
            dict["password"]      = model.Password;

            using (HttpClient http = new HttpClient())
                using (var content = new FormUrlEncodedContent(dict))
                {
                    var msg = await http.PostAsync(configuration["IdentityService:TokenUri"], content);

                    if (!msg.IsSuccessStatusCode)
                    {
                        return(StatusCode(Convert.ToInt32(msg.StatusCode)));
                    }

                    string result = await msg.Content.ReadAsStringAsync();

                    return(Content(result, "application/json"));
                }
        }
Beispiel #2
0
        public async Task <LoginResult> RequestToken([FromBody] LoginRequestParam model)
        {
            Dictionary <string, string> dict = new Dictionary <string, string>();

            dict["client_id"]     = model.ClientId;
            dict["client_secret"] = "spasecret";
            dict["grant_type"]    = "password";
            dict["username"]      = model.UserName;
            dict["password"]      = model.Password;

            LoginResult result = new LoginResult();

            var httpClient = HttpClientFactory.CreateClient();

            using (var content = new FormUrlEncodedContent(dict))
            {
                var msg = await httpClient.PostAsync("http://localhost:5100/connect/token", content);

                if (!msg.IsSuccessStatusCode)
                {
                    //return StatusCode(Convert.ToInt32(msg.StatusCode));
                    return(result);
                }

                string response = await msg.Content.ReadAsStringAsync();

                result.State = 1;
                result.Token = JsonConvert.DeserializeObject <Token>(response);

                //return Content(result, "application/json");
                return(result);
            }
        }
Beispiel #3
0
        public bool Login(LoginRequestParam param)
        {
            var user = _userDal.GetByName(param.Name);

            if (user is null)
            {
                throw new ApplicationException("The user is not exist.");
            }
            return(user.Pwd == param.Pwd);
        }
        public ResponseResult <LoginResponseResult> LoginByFromBody([FromUri] int id,
                                                                    [FromBody] LoginRequestParam loginRequestParam)
        {
            var result = ResponseResult <LoginResponseResult> .Default();

            try
            {
                if (string.IsNullOrEmpty(loginRequestParam.user_id))
                {
                    return(ResponseResult <LoginResponseResult> .Faild("user_id不能为空!"));
                }

                if (string.IsNullOrEmpty(loginRequestParam.user_password))
                {
                    return(ResponseResult <LoginResponseResult> .Faild("user_password不能为空!"));
                }

                //=>[模拟从数据库中验证user_id和user_password]
                if (loginRequestParam.user_id.Equals("abennet") && loginRequestParam.user_password.Equals("abennet"))
                {
                    LoginResponseResult entity = new LoginResponseResult
                    {
                        UserId       = loginRequestParam.user_id,
                        UserPassword = loginRequestParam.user_password,
                        LoginTime    = DateTime.Now,
                        SessionToken = Guid.NewGuid().ToString("N"),
                    };
                    result = ResponseResult <LoginResponseResult> .Success(entity, "登录成功!");
                }
                else
                {
                    result = ResponseResult <LoginResponseResult> .Faild("登录失败!");
                }
            }
            catch (System.Exception ex)
            {
                result = ResponseResult <LoginResponseResult> .Exception(ex.Message);
            }
            return(result);
        }
Beispiel #5
0
    /// <summary>
    /// ログイン
    /// </summary>
    /// <param name="Name">名前</param>
    /// <param name="Callback">コールバック</param>
    public static IEnumerator Login(string Name, Action <LoginResult> Callback)
    {
        string            URL   = BaseURL + "login";
        LoginRequestParam Param = new LoginRequestParam();

        Param.name = Name;
        using (var Req = MakePostRequest <LoginRequestParam>(URL, Param))
        {
            yield return(Req.SendWebRequest());

            if (Req.responseCode != 200)
            {
                LoginResult FailResult = new LoginResult();
                FailResult.result = false;
                Callback?.Invoke(FailResult);
                yield break;
            }

            LoginResult Result = JsonUtility.FromJson <LoginResult>(Req.downloadHandler.text);
            Callback?.Invoke(Result);
        }
    }