예제 #1
0
        public static async Task <User> CreateNewUser(BasicRegistrationDetails details)
        {
            using (GymBayContext db = new GymBayContext())
            {
                DateTime now     = DateTime.Now;
                User     newUser = new User
                {
                    CreationDate  = now,
                    ModifedDate   = now,
                    Email         = details.Email,
                    FirstName     = details.FirstName,
                    LastName      = details.LastName,
                    Username      = string.Format("{0}{1}", details.FirstName, details.LastName),
                    Status        = (int)Enums.UserStatus.Active,
                    Bio           = "",
                    AverageRating = 5d,
                    CoachBio      = "",
                };

                newUser.Username = EnsureUsernameAuthentic(newUser.Username);

                db.User.Add(newUser);

                await db.SaveChangesAsync();

                return(newUser);
            }
        }
예제 #2
0
        public async Task <BasicRegistrationDetails> ProcessLinkedInLogin(string code)
        {
            try
            {
                BasicRegistrationDetails model = new BasicRegistrationDetails();

                HttpClient client = new HttpClient();
                //client.DefaultRequestHeaders.Add("Content-Type", "x-www-form-urlencoded");

                string redirect = Request.Host.HasValue && Request.Host.Host == "localhost"
                    ? "http://localhost:59850/"
                    : "https://gym-bay.com";

                Dictionary <string, string> parameters = new Dictionary <string, string>
                {
                    { "grant_type", "authorization_code" },
                    { "code", code },
                    { "redirect_uri", redirect },
                    { "client_id", "81jgok12c4g7jl" },
                    { "client_secret", "iBRWgS57GiNZCBCU" }
                };

                var content = new FormUrlEncodedContent(parameters);

                var postResponse = await client.PostAsync("https://www.linkedin.com/oauth/v2/accessToken", content);

                string responseString = await postResponse.Content.ReadAsStringAsync();

                dynamic responseObject  = JsonConvert.DeserializeObject(responseString);
                string  accessToken     = responseObject["access_token"];
                string  expires_seconds = responseObject["expires_in"];

                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
                var dataRequest = await client.GetAsync("https://api.linkedin.com/v2/me?projection=(id,firstName,lastName,profilePicture(displayImage~:playableStreams))");

                string dataString = await dataRequest.Content.ReadAsStringAsync();

                dynamic responseObject_2 = JsonConvert.DeserializeObject(dataString);

                model.FirstName = responseObject_2["firstName"]["localized"]["en_US"] ?? "";
                model.LastName  = responseObject_2["lastName"]["localized"]["en_US"] ?? "";

                var emailRequest = await client.GetAsync("https://api.linkedin.com/v2/emailAddress?q=members&projection=(elements*(handle~))");

                string emailString = await emailRequest.Content.ReadAsStringAsync();

                dynamic emailResponseObject = JsonConvert.DeserializeObject(emailString);

                string email = emailResponseObject["elements"][0]["handle~"]["emailAddress"] ?? "";

                model.Email = email;

                return(model);
            }
            catch (Exception)
            {
                return(new BasicRegistrationDetails());
            }
        }
예제 #3
0
        public async Task <HttpResult> FacebookLogin([FromHeader] string authorisation, [FromForm] FBLogin fblogin)
        {
            try
            {
                if (Functions.CheckAuthorisation(authorisation, out string email, out _))
                {
                    var hello = Request;
                    // = (FBLogin)JsonConvert.DeserializeObject(Request.Form.Keys.First());

                    User loggingIn;
                    bool newUser = false;

                    if (db.User.Any(x => x.Email == email))
                    {
                        loggingIn = db.User.FirstOrDefault(x => x.Email == email);
                    }
                    else if (!Functions.IsEmail(email))
                    {
                        throw new Exception("Email invalid!");
                    }
                    else
                    {
                        newUser = true;
                        BasicRegistrationDetails details = new BasicRegistrationDetails
                        {
                            FirstName = fblogin.FirstName,
                            LastName  = fblogin.LastName,
                            Email     = email
                        };
                        loggingIn = await CreateNewUser(details);
                    }

                    loggingIn.SessionToken = Functions.RandomString(40);

                    db.Entry(loggingIn).State = Microsoft.EntityFrameworkCore.EntityState.Modified;

                    Response.Cookies.Append("UserID", loggingIn.Id.ToString());
                    Response.Cookies.Append("SessionID", loggingIn.SessionToken);

                    db.SaveChanges();

                    return(new HttpResult(true, new { newUser }, ""));
                }
                else
                {
                    return(new HttpResult(false, null, "Unauthorised!"));
                }
            }
예제 #4
0
        public async Task <IActionResult> LinkedInLogin(string redirectURL, string code)
        {
            BasicRegistrationDetails model = new BasicRegistrationDetails();

            bool linkedInLogin = !string.IsNullOrEmpty(code);

            if (linkedInLogin)
            {
                model = await ProcessLinkedInLogin(code);

                redirectURL = "";
            }
            else
            {
                return(View(Request));
            }

            User loggingIn;

            if (db.User.Any(x => x.Email == model.Email))
            {
                loggingIn = db.User.FirstOrDefault(x => x.Email == model.Email);
            }
            else if (!Functions.IsEmail(model.Email))
            {
                return(View(Request));
            }
            else
            {
                loggingIn = await CreateNewUser(model);
            }

            loggingIn.SessionToken = Functions.RandomString(40);

            db.Entry(loggingIn).State = Microsoft.EntityFrameworkCore.EntityState.Modified;

            Response.Cookies.Append("UserID", loggingIn.Id.ToString());
            Response.Cookies.Append("SessionID", loggingIn.SessionToken);

            await db.SaveChangesAsync();

            return(View(Request));
        }