Ejemplo n.º 1
0
        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);
        }
Ejemplo n.º 2
0
 public ActionResult Login()
 {
     var vm = new LoginViewModel();
     return this.View(vm);
 }