Esempio n. 1
0
        public async Task <IActionResult> Login([Bind("username,password")] Services.ViewModel.UserViewModel model)
        {
            Console.Write(model);
            AuthenticationModel response = null;

            System.Net.Http.HttpMethod method = System.Net.Http.HttpMethod.Post;
            if (model.Id != Guid.Empty)
            {
                method = System.Net.Http.HttpMethod.Put;
            }

            response = await ApiRequestHelper.postPutEncodedRequest <AuthenticationModel>(
                EscolaDeVoce.Backend.Helpers.EscolaDeVoceEndpoints.tokenUrl,
                model.username,
                model.password
                );

            if (response != null)
            {
                const string Issuer = "https://www.escoladevoce.com.br";
                var          claims = new List <Claim> {
                    new Claim(ClaimTypes.Name, "Charles", ClaimValueTypes.String, Issuer),
                    new Claim(ClaimTypes.Surname, "França", ClaimValueTypes.String, Issuer),
                    new Claim(ClaimTypes.Country, "BR", ClaimValueTypes.String, Issuer),
                    new Claim(ClaimTypes.Country, "BR", ClaimValueTypes.String, Issuer),
                    new Claim("TOKEN", response.access_token, ClaimValueTypes.String, Issuer),
                    new Claim("facebookid", "112345432145432", ClaimValueTypes.String, Issuer),
                    new Claim("id", Guid.NewGuid().ToString(), ClaimValueTypes.String)
                };

                var userIdentity  = new ClaimsIdentity(claims, "Passport");
                var userPrincipal = new ClaimsPrincipal(userIdentity);

                await HttpContext.Authentication.SignInAsync("Cookie", userPrincipal,
                                                             new AuthenticationProperties
                {
                    ExpiresUtc   = DateTime.UtcNow.AddMinutes(20),
                    IsPersistent = true,
                    AllowRefresh = false
                });

                return(RedirectToAction("Index", "Project"));
            }

            Console.Write(response);
            return(View());
        }
Esempio n. 2
0
        public async Task <IActionResult> Login(string username, string password, bool isFacebook)
        {
            Frontend.AuthenticationModel response = null;
            response = await ApiRequestHelper.postPutEncodedRequest <AuthenticationModel>(
                Helpers.EscolaDeVoceEndpoints.tokenUrl,
                username,
                password,
                isFacebook
                );

            if (response != null)
            {
                if (response.StatusCode != HttpStatusCode.Created)
                {
                    return(Json(new {
                        status = false
                    }));
                }
                Infrastructure.ApiResponse <EscolaDeVoce.Services.ViewModel.UserViewModel> userresponse = null;

                userresponse = await ApiRequestHelper.Get <Infrastructure.ApiResponse <EscolaDeVoce.Services.ViewModel.UserViewModel> >(
                    Helpers.EscolaDeVoceEndpoints.User.info,
                    null,
                    response.access_token
                    );

                const string Issuer = "https://www.escoladevoce.com.br";
                var          claims = new List <Claim> {
                    new Claim(ClaimTypes.Name, userresponse.data.person.name, ClaimValueTypes.String, Issuer),
                    new Claim("Id", userresponse.data.Id.ToString(), ClaimValueTypes.String, Issuer),
                    new Claim(ClaimTypes.Email, userresponse.data.username, ClaimValueTypes.String, Issuer),
                    new Claim("TOKEN", response.access_token, ClaimValueTypes.String, Issuer),
                };

                var userIdentity = new ClaimsIdentity(claims, "Passport");

                if (!String.IsNullOrEmpty(userresponse.data.cover))
                {
                    userIdentity.AddClaim(new Claim("cover", userresponse.data.cover, ClaimValueTypes.String, Issuer));
                }
                if (!String.IsNullOrEmpty(userresponse.data.image))
                {
                    userIdentity.AddClaim(new Claim("image", userresponse.data.image, ClaimValueTypes.String, Issuer));
                }

                var userPrincipal = new ClaimsPrincipal(userIdentity);

                await HttpContext.Authentication.SignInAsync("Cookie", userPrincipal,
                                                             new AuthenticationProperties
                {
                    ExpiresUtc   = DateTime.UtcNow.AddMinutes(30),
                    IsPersistent = true,
                    AllowRefresh = false
                });

                return(Json(new {
                    status = true
                }));
            }

            return(Json(new {
                status = false
            }));
        }