public static User GetSession() { if (HttpContext.Current.Session["AuthorizedUser"] != null) { return(HttpContext.Current.Session["AuthorizedUser"] as User); } if (!string.IsNullOrEmpty(HttpContext.Current.User.Identity.Name)) { // Get username from User.Indentity.Name var membership = MembershipDto.Single(HttpContext.Current.User.Identity.Name); if (membership == null) { return(null); } SetSession(membership.ID); return(HttpContext.Current.Session["AuthorizedUser"] as User); } return(null); }
public ActionResult Register(RegisterModel model) { if (ModelState.IsValid) { // Attempt to register the user var user = new MembershipDto(); object userKey; MembershipCreateStatus createStatus; user.CreateUser(model.UserName, model.Email, model.Password, out userKey, out createStatus); if (createStatus.Equals(MembershipCreateStatus.Success)) { // 注册成功后,直接将表单的用户名,存入cookie FormsAuthentication.SetAuthCookie(model.UserName, false); UserDto.SetSession(userKey); // 如果开启微信授权则跳转微信OpenUrl, 跳转首页 if (BrowserInfo.IsWeChatClient() && ConfigGlobal_Arsenal.WeChatActive) { TempData["DataUrl"] = $"data-url=/Account/WeChatLogin/?scope={ScopeType.snsapi_userinfo}"; return RedirectToAction("WeChatLogin", "Account", new { scope = ScopeType.snsapi_userinfo }); } TempData["DataUrl"] = "data-url=/"; return RedirectToAction("Index", "Home"); } ModelState.AddModelError("Warn", ErrorCodeToString(createStatus)); } // If we got this far, something failed, redisplay form return View(model); }
public ActionResult Login(LoginModel model, string returnUrl) { if (ModelState.IsValid) { Membership mem; int acnUid; var loginSuccess = false; if (MembershipDto.ValidateUser(model.UserName, out mem)) { if (mem.Password.Equals(Encrypt.GetMd5Hash(model.Password))) { // Sign in FormsAuthentication.SetAuthCookie(mem.UserName, model.RememberMe); UserDto.SetSession(mem.SignIn()); loginSuccess = true; } else { ModelState.AddModelError("Warn", "用户名或密码不正确"); } } else if (MembershipDto.ValidateAcnUser(model.UserName, model.Password, out acnUid)) { // not in SSO, but in Acn Users // Sync the user info, register SSO and then log in if (acnUid > 0) { var membership = new MembershipDto(); MembershipCreateStatus createStatus; membership.CreateAcnUser(acnUid, out createStatus); if (createStatus.Equals(MembershipCreateStatus.Success)) { FormsAuthentication.SetAuthCookie(membership.UserName, model.RememberMe); UserDto.SetSession(membership.SignIn()); loginSuccess = true; } else { ModelState.AddModelError("Warn", ErrorCodeToString(createStatus)); } } else { ModelState.AddModelError("Warn", ErrorCodeToString(MembershipCreateStatus.InvalidUserName)); } } else { ModelState.AddModelError("Warn", "用户名不存在或密码不正确"); } // 处理登录跳转,如果开启微信授权则跳转微信OpenUrl, 否则跳转返回路径, 最后跳转首页 if (loginSuccess) { if (BrowserInfo.IsWeChatClient() && ConfigGlobal_Arsenal.WeChatActive) { TempData["DataUrl"] = $"data-url=/Account/WeChatLogin/?scope={ScopeType.snsapi_base}"; return RedirectToAction("WeChatLogin", "Account", new { scope = ScopeType.snsapi_base }); } if (Url.IsLocalUrl(returnUrl)) { TempData["DataUrl"] = $"data-url={returnUrl}"; return Redirect(returnUrl); } TempData["DataUrl"] = "data-url=/"; return RedirectToAction("Index", "Home"); } } // If we got this far, something failed, redisplay form return View(model); }