public async Task<bool> IsAuthenticated(PortalUser user) { string Url = ConfigurationManager.AppSettings["URL"].ToString(); string CrmConnectionString = string.Format("Url={0}; Username={1}; Password={2}", Url, user.Username, user.Password); ClientCredentials credential = new ClientCredentials(); credential.UserName.UserName = user.Username; credential.UserName.Password = user.Password; CrmConnection crmConnection = CrmConnection.Parse(CrmConnectionString); crmConnection.ClientCredentials = credential; OrganizationService service = new OrganizationService(crmConnection); QueryExpression qe = new QueryExpression("systemuser"); qe.ColumnSet = new ColumnSet(); qe.ColumnSet.AddColumn("systemuserid"); qe.ColumnSet.AddColumn("fullname"); qe.Criteria = new FilterExpression(); qe.Criteria.AddCondition("domainname", ConditionOperator.Equal, user.Username); EntityCollection collection = service.RetrieveMultiple(qe); if (collection.Entities.Count == 0) { return false; } return true; }
public async Task<ActionResult> Index(PortalUser userInput) { try { bool result = await auth.IsAuthenticated(userInput); if (result) { SystemUser userDetails = await dbUser.GetUserDetails(userInput.Username); if (userDetails != null) { FormsAuthentication.SetAuthCookie(userDetails.ID, false); SetCookies(userInput); Session["week"] = DateExtension.CurrentWeek; return RedirectToAction("Index", "Entry"); } ViewBag.ErrorMessage = AlertMessages.Error("Error!", "Invalid Username/Password"); return View(); } ViewBag.ErrorMessage = AlertMessages.Error("Error!", "Your account doesn't exist in our records."); return View(); } catch (Exception ex) { ViewBag.ErrorMessage = AlertMessages.Error("Error!", "Invalid Username/Password"); return View(); } }
public void SetCookies(PortalUser data) { HttpCookie myCookie = new HttpCookie("UserSettings"); myCookie["week"] = DateExtension.CurrentWeek.ToString(); myCookie["domain"] = data.Username.Encrypt(); myCookie["user"] = data.Password.Encrypt(); Response.Cookies.Add(myCookie); }