public ActionResult Login(LoginViewModel model, string returnUrl)
        {
            //FormsAuthentication.SetAuthCookie("sdaniel", false);
            //Session.Add("Username", "Scott");
            //return this.RedirectToAction("Index", "Home");
            if (!this.ModelState.IsValid)
            {
                return this.View(model);
            }

            try
            {
                if (Membership.ValidateUser(model.UserName, model.Password))
                {
                    string GroupName = IRMSettings.GetSettingByName("IRMMgt.ADGroup");//"IRM Developers";//"IRM-Management";
                    var LDAPPath = IRMSettings.GetSettingByName("LDAP.Path");
                    log.DebugFormat("LDAP Path: {0}", LDAPPath);

                    var accountMgr = new AccountManagement();
                    var groupList = accountMgr.GetGroupsForUser(model.UserName, model.Password, LDAPPath);

                    foreach(var group in groupList)
                    {
                        log.DebugFormat("     Group: {0}", group);
                    }

                    log.DebugFormat("Looking for group: {0}", GroupName);


                    if (groupList.Contains(GroupName, StringComparer.OrdinalIgnoreCase) || groupList.Contains("IRM-QA", StringComparer.OrdinalIgnoreCase))
                    {
                        //string domainName = ConfigurationManager.AppSettings["Domain"];
                        string domainName = IRMSettings.GetSettingByName("AD.Domain");
                        string username = string.Format(@"{0}\{1}", domainName, model.UserName);
                        FormsAuthentication.SetAuthCookie(username, model.RememberMe);
                        Session.Add("Username", model.UserName);
                        if (this.Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
                            && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
                        {
                            return this.Redirect(returnUrl);
                        }

                        return this.RedirectToAction("Index", "Home");
                    }
                    this.ModelState.AddModelError(string.Empty, string.Format("{0} is not authorized for this site. User must be a member of group {1} or {2}", model.UserName, GroupName, "IRM - QA"));
                    return this.View(model);
                }
            }
            catch (Exception e)
            {
                log.ErrorFormat("Exception:  {0}", e.Message);
                ViewBag.Error = "Error " + e.Message;
                return this.View(model);
            }        

            this.ModelState.AddModelError(string.Empty, "The user name or password provided is incorrect.");

            return this.View(model);
        }
Exemplo n.º 2
0
        public ArrayList EnumerateUsers(string OuDn)
        { 
            ArrayList alObjects = new ArrayList();

            IRM.Library.Security.AccountManagement am = new AccountManagement();
            List<UserInformation> users = am.GetUsersForGroup("Domain Users");

            foreach (var user in users)
            {
                alObjects.Add(user.DisplayName);
            }
            
            return alObjects;
        }
        public ActionResult Login(FormCollection fc)
        {
            //FormsAuthentication.SetAuthCookie(viewModel.UserName, viewModel.RememberMe);
            ////Get the UserID value and save in the session.
            //Session.Add("UserId", "1b4b818f-277e-4dcc-8c9f-2e039e911c53");
            //return RedirectToAction("ServiceLineExplorer", "Analytics");

            if (ModelState.IsValid)
            {
                TryUpdateModel(viewModel);
                Session["UserViewModel"] = viewModel;
                log.Debug("Start Login");

                if (!this.ModelState.IsValid)
                {
                    viewModel.Password = string.Empty;
                    return this.View(viewModel);
                }

                try
                {
                    log.DebugFormat("Call ValidateUser {0}", viewModel.UserName);
                    if (Membership.ValidateUser(viewModel.UserName, viewModel.Password))
                    {
                        log.DebugFormat("User validated");

                        List<string> groupList = new List<string>();
                        var accountMgr = new AccountManagement();
                        try
                        {
                            var configConnectionString = ConfigurationManager.ConnectionStrings["ADConnectionString"].ToString();
                            var connectionString = string.Concat(configConnectionString.Split(':')[0], ":", configConnectionString.Split(':')[1]);
                            log.InfoFormat("LDAP Connectionstring:  {0}", connectionString);
                            groupList = accountMgr.GetGroupsForUser(viewModel.UserName, viewModel.Password, connectionString);

                            // Debug Hack - Add an entry to the group list so that we can run any client on LocalHost
                            if ( string.Compare(viewModel.HealthCareSystemPrefix, "localhost", StringComparison.OrdinalIgnoreCase) == 0)
                            {
                                groupList.Add("IRM-localhost");
                            }

                            log.InfoFormat("Group Count = {0}", groupList.Count.ToString());
                        }
                        catch (Exception e)
                        {
                            viewModel.ErrorMsg = e.Message;
                            log.ErrorFormat("Exception:  {0}", e.Message);
                            viewModel.Password = string.Empty;
                            return RedirectToAction("Login", "UserAccount");
                        }

                        try
                        {
                            foreach(var group in groupList)
                            {
                                log.InfoFormat("Group Name:  {0}", group);
                            }
                        }
                        finally { }
                        
                        if (
                            groupList.Contains(string.Concat("IRM-", viewModel.HealthCareSystemPrefix), StringComparer.OrdinalIgnoreCase) ||
                            groupList.Contains("IRM-QA", StringComparer.OrdinalIgnoreCase)
                           )
                        {
                            FormsAuthentication.SetAuthCookie(viewModel.UserName, viewModel.RememberMe);
                            //Get the UserID value and save in the session.
                            Session.Add("UserId", "1b4b818f-277e-4dcc-8c9f-2e039e911c53");
                            return RedirectToAction("ServiceLineExplorer", "Analytics");
                        }
                        else
                        {
                            viewModel.ErrorMsg = "The username and password are valid but not authorized for this site.";                            
                        }

                    }
                    else
                    {
                        viewModel.ErrorMsg = "Login was unsuccessful. The user name or password provided is incorrect.";
                    }
                }
                catch (Exception e)
                {
                    log.ErrorFormat("Exception:  {0}", e.Message);
                    viewModel.ErrorMsg = "Error " + e.Message;
                    viewModel.Password = string.Empty;
                    return RedirectToAction("Login", "UserAccount");
                }
                viewModel.ErrorMsg = "Login was unsuccessful. The user name or password provided is incorrect.";
            }

            return RedirectToAction("Login", "UserAccount");
        }