/// <summary> /// Runs the self process in in local system credential. /// </summary> /// <param name="parameters">The parameters.</param> /// <param name="credential">The credential.</param> private void RunSelfProcessInLocalSystem(List <string> parameters, NetworkCredential credential) { WindowsStationAndDesktop.GrantAccess(credential.UserName); using (new PrivilegeEnabler(Process.GetCurrentProcess(), Privilege.AssignPrimaryToken, Privilege.IncreaseQuota, Privilege.TrustedComputerBase)) using (WindowsImpersonation imp = new WindowsImpersonation(credential.Domain, credential.UserName, credential.Password.ConvertToSecureString())) { _childPID = imp.RunCommand(System.Reflection.Assembly.GetEntryAssembly().Location, " " + string.Join(" ", parameters.ToArray()), Environment.CurrentDirectory, false).Id; } Log("PID CHILD : " + _childPID); // Wait until child process exit. HasExited = false; while (!HasExited) { // Check if the process has exited try { Process.GetProcessById(_childPID); } catch { HasExited = true; } Thread.Sleep(100); } }
public ActionResult Login(LoginModel model, string ReturnUrl) { if (model.UserName != null) { model.UserName = model.UserName.Trim(); } if (model.Password != null) { model.Password = model.Password.Trim(); } log.Debug(string.Format("Begin Login({0}:{1}, {2})", model.UserName, model.RememberMe, ReturnUrl)); try { var cusRelUser = ServicesProxy.RequestState.UserDetails; if (cusRelUser == null) { InitRequestState(model.UserName); cusRelUser = ServicesProxy.RequestState.UserDetails; } var modelState = ModelState.IsValid; var isEmail = model.UserName != null && model.UserName.Contains("@"); var loginResult = false; var retries = 3; while (retries > 0) { try { loginResult = WindowsImpersonation.Login(model.UserName, model.Password); retries = -1; } catch (Exception e) { log.Error(string.Format("WindowsImpersonation.Login: {0}, Retries: {1}", e.Message, retries)); retries -= 1; } } log.Debug(string.Format("ModelState.IsValid: {0}, loginResult: {1}, isEmail: {2}, cusRelUser: {3}", modelState, loginResult, isEmail, cusRelUser != null ? cusRelUser.Username : null)); if (cusRelUser == null || isEmail || !modelState || !loginResult) { ViewBag.ReturnUrl = ReturnUrl; var error = cusRelUser == null || model.UserName == null ? @"User not found. Please contact [email protected] to obtain access." : (isEmail ? @"Please use your Windows username instead of email address." : (!modelState ? @"Please enter both User Name (not email) and Password." : @"The UserName or Password provided is incorrect.")); log.Debug(string.Format("ModelState.IsValid Error: {0}, ReturnUrl: {1}", error, ReturnUrl)); ModelState.AddModelError("", error); return(View(model)); } log.Debug("SetAuthCookie"); FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, model.UserName, DateTime.Now, DateTime.Now.AddDays(180), model.RememberMe, "", FormsAuthentication.FormsCookiePath); string encTicket = FormsAuthentication.Encrypt(ticket); HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket); cookie.Expires = DateTime.Now.AddDays(180); Response.Cookies.Add(cookie); CustomPrincipal newUser = new CustomPrincipal(model.UserName); HttpContext.User = newUser; return(Redirect(ReturnUrl ?? "../Home")); } finally { log.Debug(string.Format("End Login({0}:{1}, {2})", model.UserName, model.RememberMe, ReturnUrl)); } }