コード例 #1
0
        public async Task <Response> RegisterUser([FromForm] VMRegisterUser user)
        {
            VMRegisterUser register = new VMRegisterUser();

            return(await register.RegisterUser(user));
        }
コード例 #2
0
        public async Task <Response> RegisterUser([FromForm] VMRegisterUser user)
        {
            using (HttpClient clientc = new HttpClient())
            {
                VMLoginApi userToRegister = new VMLoginApi
                {
                    username = user.Email,
                    password = user.Password
                };
                using (var response = await clientc.PostAsync("http://infinitysmartapi-dev.azurewebsites.net/api/auth", new StringContent(JsonConvert.SerializeObject(userToRegister), Encoding.UTF8, ContentType.Json)))
                {
                    using (var content = response.Content)
                    {
                        var result = await content.ReadAsStringAsync();

                        var responseBody = result;
                        if (response.StatusCode == HttpStatusCode.OK || response.StatusCode == HttpStatusCode.Created)
                        {
                            var    ds      = JsonConvert.DeserializeObject(responseBody);
                            var    jobject = (JObject)JsonConvert.DeserializeObject(responseBody);
                            var    jvalue  = (JValue)jobject["token"];
                            string token   = jvalue.Value.ToString();
                            if (token != null)
                            {
                                HttpContext.Session.SetString("JWToken", token);
                                HttpContext.Session.SetString("userEmail", user.Email);
                                var claims = new List <Claim>
                                {
                                    new Claim(ClaimTypes.Name, user.Email),
                                };

                                var claimsIdentity = new ClaimsIdentity(
                                    claims, CookieAuthenticationDefaults.AuthenticationScheme);

                                //cookies adjustment
                                CookieOptions option = new CookieOptions();

                                option.Expires = DateTime.Now.AddDays(1);
                                Response.Cookies.Append("userID", Convert.ToString(user.Email), option);
                                //cookies adjustment
                                await HttpContext.SignInAsync(
                                    CookieAuthenticationDefaults.AuthenticationScheme,
                                    new ClaimsPrincipal(claimsIdentity));

                                return(new Response()
                                {
                                    IsSuccess = true,
                                    Message = "Registered and Logged in"
                                });
                            }
                            else
                            {
                                return(new Response()
                                {
                                    IsSuccess = false,
                                    Message = "Not registered"
                                });
                            }
                        }
                        else
                        {
                            return(new Response()
                            {
                                IsSuccess = false,
                                Message = "Something went wrong"
                            });
                        }
                    }
                }
            }
        }