Пример #1
0
        /// <summary>
        /// Creates the user.
        /// </summary>
        /// <param name="userDto">The user dto.</param>
        public async Task CreateUser(OktaUserDto userDto)
        {
            var createUser = new CreateOktaUser
            {
                Profile  = userDto,
                GroupIds = new string[] { }
            };

            var payload = JsonConvert.SerializeObject(createUser, new JsonSerializerSettings
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            });
            var url      = $"{Url}?activate=false";
            var content  = new StringContent(payload, Encoding.UTF8, "application/json");
            var response = await httpClient.PostAsync(url, content);
        }
Пример #2
0
        public ActionResult EditUser(UserModel user)
        {
            OKTAServiceRepository okta = new OKTAServiceRepository();
            var Okta         = okta.GetUserProfile(HttpContext.GetOwinContext().Authentication.User.Claims);
            var IsSuperAdmin = db.IsSuperAdmin(Okta.email);

            if (!IsSuperAdmin)
            {
                ViewBag.RoleExistMessage = "Please contact HANYS Admin to edit";
            }

            if (ModelState.IsValid && !string.IsNullOrEmpty(user.USR_Email) && !IsUserExists(user) && ValidateHospital(user.USR_OrganizationName) && IsSuperAdmin)
            {
                ServiceRepository serviceObj = new ServiceRepository();
                user.USR_Active = true;
                HttpResponseMessage response = serviceObj.PutResponse("user/PutUserMaster", user);



                var client = new HttpClient();
                //assign the API URL call
                client.BaseAddress = new Uri(APIuri + "/v1/");
                //Set up the header for the API call
                client.DefaultRequestHeaders.Clear();
                //set up the API header by including the API key located at web.config
                client.DefaultRequestHeaders.Add("Authorization", "SSWS " + APIkey);

                client.DefaultRequestHeaders.Accept.Add(
                    new MediaTypeWithQualityHeaderValue("application/json"));



                CreateOktaUser OktaUser = new CreateOktaUser();

                OktaUser.email       = user.USR_Email;
                OktaUser.firstName   = user.USR_FirstName;
                OktaUser.lastName    = user.USR_LastName;
                OktaUser.login       = user.USR_Email;
                OktaUser.secondEmail = null;
                OktaUser.middleName  = null;
                OktaUser.mobilePhone = null;


                string json = Newtonsoft.Json.JsonConvert.SerializeObject(OktaUser);

                json = json.Replace("{", "{\"profile\": {");
                json = json.Replace("}", "}}");
                var httpContent = new StringContent(json, Encoding.Default, "application/json");

                string Postuser;
                Postuser = "******" + user.USR_OKTAID;
                //Call the Okta apps API to get a list of all available apps
                response = client.PostAsync(Postuser, httpContent).Result;

                //initialized couple of variables
                var jsonResponse = "";
                response.Content.ToString();

                if (response.IsSuccessStatusCode)
                {
                    //if the call was succesful get the results
                    jsonResponse = response.Content.ReadAsStringAsync().Result;
                    //Message on successful user creation
                    //var messageModel = new MessageModel();
                }


                return(RedirectToAction("Index", "Userroles", new { Id = user.USR_Id }));
            }
            else
            {
                return(View(user));
            }
        }
Пример #3
0
        public ActionResult Create(UserModel user)
        {
            OKTAServiceRepository okta = new OKTAServiceRepository();
            var Okta         = okta.GetUserProfile(HttpContext.GetOwinContext().Authentication.User.Claims);
            var IsSuperAdmin = db.IsSuperAdmin(Okta.email);

            if (!IsSuperAdmin)
            {
                ViewBag.RoleExistMessage = "Please contact HANYS Admin to create";
            }

            // UserServiceRepository serviceObj = new UserServiceRepository();
            // user.USR_Active = true;
            //HttpResponseMessage response = serviceObj.PostResponse("user/PostUserMaster", user);
            // response.EnsureSuccessStatusCode();
            int flag = 0;

            // return RedirectToAction("GetUsers");
            if (ModelState.IsValid && !string.IsNullOrEmpty(user.USR_Email) && !IsUserExists(user) && ValidateHospital(user.USR_OrganizationName) && IsSuperAdmin)
            {
                ServiceRepository serviceObj = new ServiceRepository();
                user.USR_Active = true;

                HttpResponseMessage response = serviceObj.PostResponse("user/PostUserMaster", user);
                response.EnsureSuccessStatusCode();

                var result = response.Content.ReadAsStringAsync().Result;
                user = JsonConvert.DeserializeObject <UserModel>(result);



                if (response.StatusCode == System.Net.HttpStatusCode.Created)
                {
                    var client = new HttpClient();
                    //assign the API URL call
                    client.BaseAddress = new Uri(APIuri + "/v1/");
                    //Set up the header for the API call
                    client.DefaultRequestHeaders.Clear();
                    //set up the API header by including the API key located at web.config
                    client.DefaultRequestHeaders.Add("Authorization", "SSWS " + APIkey);

                    client.DefaultRequestHeaders.Accept.Add(
                        new MediaTypeWithQualityHeaderValue("application/json"));

                    string Geturl = "";

                    Geturl = "users?search=profile.email eq \"" + user.USR_Email + "\"";


                    var response_okta = client.GetAsync(Geturl).Result;
                    //initialized couple of variables
                    var jsonResponse = "";

                    List <Web.Models.OktaUser.Users> user_okta = new List <Web.Models.OktaUser.Users>();
                    //user_okta = new Web.Models.OktaUser.Users();
                    if (response_okta.IsSuccessStatusCode)
                    {
                        //if the call was succesful get the results
                        jsonResponse = response_okta.Content.ReadAsStringAsync().Result;

                        if (jsonResponse.Length > 2)
                        {
                            //deserialized the JSON response into a list
                            user_okta = Newtonsoft.Json.JsonConvert.
                                        DeserializeObject <List <Web.Models.OktaUser.Users> >(jsonResponse);


                            if (user_okta[0].status != "ACTIVE")
                            {
                                string PostuserActivation;
                                PostuserActivation = "users/" + user_okta[0].id + "/lifecycle/activate?sendEmail=true";

                                string jsonActive = "";

                                var httpContent = new StringContent(jsonActive, Encoding.Default, "application/json");

                                response = client.PostAsync(PostuserActivation, httpContent).Result;
                                //initialized couple of variables
                                jsonResponse = "";
                                response.Content.ToString();

                                if (response.IsSuccessStatusCode)
                                {
                                    //if the call was succesful get the results
                                    jsonResponse = response.Content.ReadAsStringAsync().Result;
                                    //Message on successful user creation
                                    //var messageModel = new MessageModel();
                                }
                            }

                            // assign user to pfp application

                            string Postusergrp;
                            Postusergrp = "groups/" + PFPGroupID + "/users/" + user_okta[0].id;



                            string jsonuser = "";

                            StringContent httpContentuser = new StringContent(jsonuser, Encoding.Default, "application/json");

                            response = client.PutAsync(Postusergrp, httpContentuser).Result;
                            //initialized couple of variables
                            jsonResponse = "";
                            response.Content.ToString();

                            if (response.IsSuccessStatusCode)
                            {
                                //if the call was succesful get the results
                                jsonResponse = response.Content.ReadAsStringAsync().Result;
                                //Message on successful user creation
                                //var messageModel = new MessageModel();
                            }
                        }
                        else
                        {
                            // New user Creation in OKTA



                            CreateOktaUser OktaUser = new CreateOktaUser();



                            OktaUser.email     = user.USR_Email;
                            OktaUser.firstName = user.USR_FirstName;
                            OktaUser.lastName  = user.USR_LastName;
                            OktaUser.login     = user.USR_Email;


                            string json = Newtonsoft.Json.JsonConvert.SerializeObject(OktaUser);

                            json = json.Replace("{", "{\"profile\": {");
                            json = json.Replace("}", "}}");
                            var httpContent = new StringContent(json, Encoding.Default, "application/json");

                            string Postuser;
                            Postuser = "******";
                            //Call the Okta apps API to get a list of all available apps
                            response = client.PostAsync(Postuser, httpContent).Result;
                            //initialized couple of variables
                            jsonResponse = "";
                            response.Content.ToString();

                            if (response.IsSuccessStatusCode)
                            {
                                //if the call was succesful get the results
                                jsonResponse = response.Content.ReadAsStringAsync().Result;
                                //Message on successful user creation
                                //var messageModel = new MessageModel();
                            }

                            var obj = JObject.Parse(jsonResponse);
                            var ID  = (string)obj["id"];

                            // assign user to pfp application

                            string Postusergrp;
                            Postusergrp = "groups/" + PFPGroupID + "/users/" + ID;



                            string jsonuser = "";

                            StringContent httpContentuser = new StringContent(jsonuser, Encoding.Default, "application/json");

                            response = client.PutAsync(Postusergrp, httpContentuser).Result;
                            //initialized couple of variables
                            jsonResponse = "";
                            response.Content.ToString();

                            if (response.IsSuccessStatusCode)
                            {
                                //if the call was succesful get the results
                                jsonResponse = response.Content.ReadAsStringAsync().Result;
                                //Message on successful user creation
                                //var messageModel = new MessageModel();
                            }
                        }
                    }
                }

                return(RedirectToAction("Index", "Userroles", new { Id = user.USR_Id }));
            }
            else
            {
                return(View(user));
            }
        }