Пример #1
0
        public async Task <ActionResult> LaunchCourse(int CourseId, string code)
        {
            TenantRepository tr   = new TenantRepository();
            var     tenantList    = tr.VerifyTenantDomain(Request.Url.Host);
            TblUser sessionUser   = (TblUser)Session["UserSession"];
            var     courseBaseURL = System.Configuration.ConfigurationManager.AppSettings["CourseBaseURL"];

            if (sessionUser == null)
            {
                if (string.IsNullOrEmpty(code))
                {
                    CourseSession courseSession = new CourseSession();
                    courseSession.CourseId = CourseId;
                    Session["CourseId"]    = CourseId;
                    return(Redirect("https://pumplace.plek.co/oidc/auth?client_id=1663f8ca-7b8a-42eb-ae3b-845e32be1baf&scope=openid&response_type=code&redirect_uri=https://quiz.rockettech.co.nz/"));
                }
                else
                {
                    using (var client = new HttpClient())
                    {
                        client.DefaultRequestHeaders.Accept.Clear();
                        client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
                        client.DefaultRequestHeaders.Add("Access-Control-Allow-Origin", "*");
                        string tokenApi = "https://pumplace.plek.co/oidc/token";
                        var    values   = new Dictionary <string, string>();
                        values.Add("client_id", "1663f8ca-7b8a-42eb-ae3b-845e32be1baf");
                        values.Add("client_secret", "TZD4Jr837e6uymsJdyxH2jk8qxGQiK7gXDCmYqtC");
                        values.Add("grant_type", "authorization_code");
                        values.Add("code", code);
                        values.Add("redirect_uri", "https://quiz.rockettech.co.nz/");

                        var content = new FormUrlEncodedContent(values);

                        var responseTask = await client.PostAsync(tokenApi, content);

                        var result = await responseTask.Content.ReadAsStringAsync();

                        var    data  = (JObject)JsonConvert.DeserializeObject(result);
                        string token = data["access_token"].Value <string>();//Access Token Received

                        //Now get User Details
                        string userApi = "https://pumplace.plek.co/oidc/me";

                        client.DefaultRequestHeaders.Accept.Clear();
                        client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
                        client.DefaultRequestHeaders.Add("Authorization", "Bearer " + token);

                        HttpResponseMessage response = await client.GetAsync(userApi);

                        if (response.IsSuccessStatusCode)
                        {
                            string jsondata = await response.Content.ReadAsStringAsync();

                            var    userData = (JObject)JsonConvert.DeserializeObject(jsondata);
                            string email    = userData["email"].Value <string>();
                            string name     = userData["username"].Value <string>();

                            var userId = ur.IsUserExist(email, Request.Url.Host);

                            if (userId == 0)
                            {
                                try
                                {
                                    //Create user
                                    TblUser newUser = new TblUser();
                                    newUser.FirstName = name;
                                    newUser.EmailId   = email;
                                    newUser.RoleId    = 3;
                                    newUser.IsActive  = true;
                                    newUser.CreatedBy = 1;
                                    CommonFunctions common = new CommonFunctions();
                                    newUser.Password = common.GetEncodePassword("123456");


                                    newUser.TenantId = tenantList[0].TenantId;

                                    var newUserId = ur.AddUser(newUser);
                                    var assign    = cr.AssignCourse(CourseId, newUserId, null);
                                    userId = newUserId;
                                }
                                catch (Exception ex)
                                {
                                    newException.AddException(ex);
                                }
                            }
                            else
                            {
                                try
                                {
                                    var isAssigned = cr.CheckCourseAssignedUser(CourseId, userId);
                                    if (!isAssigned)
                                    {
                                        var assign = cr.AssignCourse(CourseId, userId, null);
                                    }
                                }
                                catch (Exception ex)
                                {
                                    newException.AddException(ex);
                                }
                            }

                            var urlNew = courseBaseURL + "?AID=" + CourseId + "&LID=" + userId;
                            return(Redirect(urlNew));
                            //return Content("<script>window.open('" + urlNew + "','_blank')</script>");
                        }
                    }
                }
            }

            List <tblCourse>  CourseDetails = new List <tblCourse>();
            CoursesRepository courseRepo    = new CoursesRepository();

            CourseDetails = courseRepo.GetCourseById(CourseId);
            var url = courseBaseURL + "?AID=" + CourseId + "&LID=" + sessionUser.UserId;

            CourseDetails[0].ContentModuleURL = url;
            if (tenantList[0].TenantId == 6)
            {
                return(Content("<script>window.open('" + url + "','_blank')</script>"));
            }
            else
            {
                return(View(CourseDetails[0]));
            }

            //return Redirect(url);
            //return Content("<script>window.open('" + url + "','_blank')</script>");
        }