public string TokenRevocation(RevocationDTO revocationDTO) { try { ValidationResult results1 = userloginvalidation.Validate(revocationDTO.user); ValidationResult results2 = refreshvalidation.Validate(revocationDTO.refresh); string refresh_token = HttpUtility.UrlDecode(revocationDTO.token); Authorize authorize = oauth.Authorize.SingleOrDefault(x => x.Code == refresh_token); User user = oauth.User.Where(x => x.UserId == authorize.UserId).FirstOrDefault(); UserDTO userLoginDTO = mapper.Map <UserDTO>(user); //Check user is authenticated var handler = new UserAuthenticationHandler(); handler.Handle(userLoginDTO); revocationDTO.user = userLoginDTO; //Check refresh token provided is real var refreshhandler = new RefreshTokenAuthenticationHandler(); refreshhandler.Handle(revocationDTO); //Set the refresh token to null authorize.Code = null; oauth.SaveChanges(); return(TokenConstants.RevokedToken); } catch (InvalidTokenException) { throw; } catch (InvalidUserException) { throw; } catch (Exception ex) { Log.Log.Error(ex, TokenConstants.InvalidUser); throw new InvalidUserException(TokenConstants.InvalidUser); } }
public UserDTO Login(UserDTO userLogin) { ValidationResult results = userloginvalidation.Validate(userLogin); User user = oauth.User.Where(x => x.UserName == userLogin.UserName).FirstOrDefault(); UserDTO userLoginDTO = mapper.Map <UserDTO>(user); userLoginDTO.password = userLogin.password; var handler = new UserAuthenticationHandler(); handler.Handle(userLoginDTO); userLoginDTO.IsAuthenticated = true; return(userLoginDTO); }
/// <summary> /// 身份验证。 /// </summary> /// <param name="authen"></param> /// <param name="account"></param> /// <param name="password"></param> /// <returns></returns> public void Authentication(EnumUserAuthen authen, string account, string password, UserAuthenticationHandler handler) { try { if (string.IsNullOrEmpty(account)) throw new ArgumentNullException("account", "账号为空!"); if (string.IsNullOrEmpty(password)) throw new ArgumentNullException("password", "密码为空!"); this.RaiseChanged("开始连接服务器,请稍后..."); this.poxy.BeginVerifyUserIdentity((int)authen, account, password, new AsyncCallback(delegate(IAsyncResult callback) { this.RaiseChanged("等待服务器返回,请稍后..."); try { Impl.CallResult callResult = this.poxy.EndVerifyUserIdentity(callback); this.RaiseChanged("已返回数据,开始分析..."); if (callResult.ResultCode == 0) { if (handler != null) { string[] arr = callResult.ResultMessage.Split(','); if (arr != null && arr.Length >= 3) { LocalUserInfo info = new LocalUserInfo(); info.SchoolID = this.cert.SchoolID; info.UserAccount = account; info.Password = password; info.UserID = arr[0]; info.UserCode = arr[1]; info.UserName = arr[2]; handler(info, null); } } this.RaiseChanged("身份验证通过..."); } else { string err = "身份验证失败," + callResult.ResultMessage; handler(null, new Exception(err)); } } catch (Exception) { this.RaiseChanged("发生异常"); handler(null, new Exception("发生网络异常")); } }), null); } catch (Exception e) { this.RaiseChanged("发生异常:" + e.Message); handler(null, new Exception("发生网络异常")); } }