/// <summary> /// 用户注册,使用翱翔门户的账户名和密码创建一个账户 /// </summary> /// <param name="input">见 `CreateUserInput` </param> /// <returns>返回用户信息,见 `UserInfoDto`</returns> /// <exception cref="UserFriendlyException"></exception> public async Task <Pair <long, UserInfoDto> > Register(RegisterInput input) { if (_userManager.Query().Any(u => u.UserInfo != null && u.UserInfo.StudentNumber == input.Username)) { throw new UserFriendlyException("User Exists"); } var userInfoDto = await new Crawler(input.Username, input.Password).GetUserInfo(); // true means: Assumed email address is always confirmed. var email = userInfoDto.StudentNumber + "@temp.mail.com"; var user = await _userRegistrationManager.RegisterAsync( userInfoDto.Name, "", email, input.Username, input.Password, true); // Create UserInfo var userInfo = ObjectMapper.Map <UserInfo>(userInfoDto); await _userInfoManager.Create(userInfo); // disable lock user.IsLockoutEnabled = false; // update userinfo user.UserInfo = userInfo; await _userManager.UpdateAsync(user); await CurrentUnitOfWork.SaveChangesAsync(); // return DTO return(new Pair <long, UserInfoDto>(user.Id, userInfoDto)); }
protected override void Initialize(System.Web.Routing.RequestContext requestContext) { base.Initialize(requestContext); #region weixin //ExternalLoginInfo loginInfo = AuthManager.GetExternalLoginInfo(); //var user = UserManager.Find(loginInfo.Login); var code = WebHelper.GetQueryString("code"); var state = WebHelper.GetQueryString("state"); _workContext.openId = WebUtils.GetCookie("openid"); if (!string.IsNullOrEmpty(code) && !string.IsNullOrEmpty(state) && state == this.WorkContext.WeiXinMPConfig.AuthorizeState) { OAuthAccessTokenResult result = null; try { result = GetOAuthAccessTokenResult(code); } catch (Exception) { } if (result != null && result.errcode == ReturnCode.请求成功) { _workContext.openId = result.openid; } WebUtils.SetCookie("openid", _workContext.openId); var access_token = GetToken(); OAuthUserInfo userInfo = OAuthApi.GetUserInfo(access_token, _workContext.openId); var wxUserInfoManager = new UserInfoManager(); var wxUserInfo = wxUserInfoManager.FindById(_workContext.openId); if (wxUserInfo == null) { wxUserInfo = new WeiXinUserInfo() { OrganizationId = _workContext.UserInfo.OrganizationId, }; FillOAuthUserInfoToWxUserInfo(userInfo, wxUserInfo); wxUserInfoManager.Create(wxUserInfo); } else { FillOAuthUserInfoToWxUserInfo(userInfo, wxUserInfo); wxUserInfoManager.Update(wxUserInfo); } this._workContext.WxUserInfo = wxUserInfo; } //测试用 #if DEBUG //if (string.IsNullOrWhiteSpace(_workContext.openId) && Request.Url.Host.ToLower().Equals("localhost")) //{ // _workContext.openId = "ozZZ5t_VheKVfHlv03srm6ylieyU"; // WebUtils.SetCookie("openid", _workContext.openId); //} #endif if (this._workContext.WxUserInfo == null && !string.IsNullOrEmpty(_workContext.openId)) { var wxUserInfoManager = new UserInfoManager(); this._workContext.WxUserInfo = wxUserInfoManager.FindById(_workContext.openId); } //UserInfo if (!requestContext.HttpContext.User.Identity.IsAuthenticated && !string.IsNullOrEmpty(_workContext.openId)) { var bindMng = new UserBindManager(); var userId = bindMng.GetUserId(_workContext.openId); if (!string.IsNullOrEmpty(userId)) { _workContext.UserInfo = UserManager.FindById(userId); SignInManager.SignInAsync(_workContext.UserInfo, isPersistent: true, rememberBrowser: true); } else { string randomEmail = string.Format("{0}@xh2005.com", Guid.NewGuid()); var user = new ApplicationUser { OrganizationId = "DebugOrganizationID", UserName = randomEmail, Email = randomEmail }; if (this._workContext.WxUserInfo != null) { user.NiceName = this._workContext.WxUserInfo.NickName; } var result = UserManager.Create(user, Guid.NewGuid().ToString()); if (result.Succeeded) { bindMng.BindUser(user.Id, _workContext.openId); SignInManager.SignIn(user, isPersistent: true, rememberBrowser: true); } _workContext.UserInfo = user; } //var access_token = GetToken(); //OAuthUserInfo userInfo = OAuthApi.GetUserInfo(access_token, _workContext.openId); //var wxUserInfoManager = new UserInfoManager(); //var wxUserInfo = wxUserInfoManager.FindById(_workContext.openId); //if (wxUserInfo == null) //{ // wxUserInfo = new WeiXinUserInfo() // { // OrganizationId = _workContext.UserInfo.OrganizationId, // }; // FillOAuthUserInfoToWxUserInfo(userInfo, wxUserInfo); // wxUserInfoManager.Create(wxUserInfo); //} //else { // FillOAuthUserInfoToWxUserInfo(userInfo, wxUserInfo); // wxUserInfoManager.Update(wxUserInfo); //} //var claims = new List<Claim>(); //claims.Add(new Claim(ClaimTypes.NameIdentifier, userId)); //claims.Add(new Claim(ClaimTypes.Name, _workContext.UserInfo.UserName)); //claims.Add(new Claim(ClaimTypes.Sid, _workContext.openId)); //var identity = new ClaimsIdentity(claims, "weixin"); //var principal = new ClaimsPrincipal(identity); //requestContext.HttpContext.User = principal; } else if (requestContext.HttpContext.User.Identity.IsAuthenticated) { _workContext.UserInfo = UserManager.FindByName(requestContext.HttpContext.User.Identity.Name); _workContext.UserId = requestContext.HttpContext.User.Identity.GetUserId(); _workContext.UserName = requestContext.HttpContext.User.Identity.GetUserName(); var bindMng = new UserBindManager(); _workContext.openId = bindMng.GeOpenId(_workContext.UserId); WebUtils.SetCookie("openid", _workContext.openId); } #endregion #region workcontext //Reqeust _workContext.IsHttpAjax = WebHelper.IsAjax(); _workContext.IP = WebHelper.GetIP(); _workContext.Url = WebHelper.GetUrl(); _workContext.UrlReferrer = WebHelper.GetUrlReferrer(); //当前控制器类名 _workContext.Controller = requestContext.RouteData.Values["controller"].ToString().ToLower(); //当前动作方法名 _workContext.Action = RouteData.Values["action"].ToString().ToLower(); _workContext.PageKey = string.Format("/{0}/{1}", _workContext.Controller, _workContext.Action); #endregion }