/// <summary>Get the access-token by username and password</summary> /// <param name="parameters"></param> /// <param name="_repo"></param> /// <param name="_settings"></param> public ResponseData DoPassword(Parameters parameters, IRTokenRepository _repo, IOptions <Audience> _settings) { var refresh_token = Guid.NewGuid().ToString().Replace("-", ""); var rToken = new RToken { ClientId = parameters.username, RefreshToken = refresh_token, Id = Guid.NewGuid().ToString(), IsStop = 0 }; if (_repo.AddToken(rToken).Result) { dynamic UserInfo = new System.Dynamic.ExpandoObject(); UserInfo.FirstName = parameters.fullname; UserInfo.UserName = parameters.username; return(new ResponseData { Code = "999", Message = "OK", Content = UserInfo, Data = GetJwt(parameters.username, refresh_token, _settings) }); } else { return(new ResponseData { Code = "909", Message = "can not add token to database", Data = null }); } }
/// <summary></summary> /// <param name="settings"></param> /// <param name="repo"></param> public TokenController(IOptions <Audience> settings, IRTokenRepository repo) { _client = MH.GetClient(); auth_db = _client.GetDatabase("AuthDB"); users_collection = auth_db.GetCollection <BsonDocument>("Users"); this._settings = settings; this._repo = repo; }
public AuthController( IConfiguration configuration, IUserServices userServices, IRTokenRepository tokenRepository) { _configuration = configuration; _userServices = userServices; _tokenRepository = tokenRepository; }
/// <summary></summary> /// <param name="settings"></param> /// <param name="repo"></param> public AuthController(IOptions <Audience> settings, IRTokenRepository repo) { _client = MH.GetClient(); auth_db = _client.GetDatabase("AuthDB"); users_collection = auth_db.GetCollection <BsonDocument>("Users"); usersCollection = auth_db.GetCollection <RegisterModel>("Users"); log_db = _client.GetDatabase("LogDB"); activitylog_collection = log_db.GetCollection <ActivityLoggerModel>("ActivityLog"); this._settings = settings; this._repo = repo; }
/// <summary>Get the access_token by refresh_token</summary> /// <param name="parameters"></param> /// <param name="_repo"></param> /// <param name="_settings"></param> public ResponseData DoRefreshToken(Parameters parameters, IRTokenRepository _repo, IOptions <Audience> _settings) { var token = _repo.GetToken(parameters.refresh_token, parameters.client_id).Result; if (token == null) { return(new ResponseData { Code = "905", Message = "can not refresh token", Data = null }); } if (token.IsStop == 1) { return(new ResponseData { Code = "906", Message = "refresh token has expired", Data = null }); } var refresh_token = Guid.NewGuid().ToString().Replace("-", ""); token.IsStop = 1; var updateFlag = _repo.ExpireToken(token).Result; var addFlag = _repo.AddToken(new RToken { ClientId = parameters.client_id, RefreshToken = refresh_token, Id = Guid.NewGuid().ToString(), IsStop = 0 }); if (updateFlag && addFlag.Result) { return(new ResponseData { Code = "999", Message = "OK", Data = GetJwt(parameters.client_id, refresh_token, _settings) }); } else { return(new ResponseData { Code = "910", Message = "can not expire token or a new token", Data = null }); } }
/// <summary>Get the access-token by username and password</summary> /// <param name="parameters"></param> /// <param name="_repo"></param> /// <param name="_settings"></param> public ResponseData DoPassword(Parameters parameters, IRTokenRepository _repo, IOptions <Audience> _settings) { try { var refresh_token = Guid.NewGuid().ToString().Replace("-", ""); var rToken = new RToken { ClientId = parameters.username, RefreshToken = refresh_token, Id = Guid.NewGuid().ToString(), IsStop = 0 }; if (_repo.AddToken(rToken).Result) { dynamic UserInfo = new System.Dynamic.ExpandoObject(); UserInfo.FirstName = parameters.fullname; UserInfo.UserName = parameters.username; return(new ResponseData { Code = "999", Message = "OK", Content = UserInfo, Data = GetJwt(parameters.username, refresh_token, _settings, BsonSerializer.Deserialize <RegisterModel>(MongoHelper.CheckForDatas("UserName", parameters.username, null, null, "Authentication", "Authentication")).UserRole) }); } else { return(new ResponseData { Code = "909", Message = "can not add token to database", Data = null }); } } catch (Exception ex) { LoggerDataAccess.CreateLog("AuthHelper", "DoPassword", ex.Message); return(new ResponseData { Code = "400", Message = "Failed", Data = null }); } }
public void Auth(IOptions <Audience> settings, IRTokenRepository repo) { //Arrage Parameters parameters = new Parameters(); parameters.grant_type = "password"; parameters.username = "******"; parameters.fullname = "Sample User"; TokenController controller = new TokenController(settings, repo); //Act var result = controller.Auth(parameters) as ActionResult; var viewResult = result.ToBsonDocument(); var filter = viewResult["Value"].AsBsonDocument; var code = filter["Code"].AsString; var message = filter["Message"].AsString; //Assert Assert.IsNotNull(result); Assert.AreEqual("999", code); }
/// <summary></summary> /// <param name="settings"></param> /// <param name="repo"></param> public TokenController(IOptions <Audience> settings, IRTokenRepository repo) { this._settings = settings; this._repo = repo; }
public Integrationtest_AuthorizedServerController_Helper(IOptions <Audience> settings, IRTokenRepository repo) { _settings = settings; _repo = repo; }
/// <summary>Get the access_token by refresh_token</summary> /// <param name="parameters"></param> /// <param name="_repo"></param> /// <param name="_settings"></param> public ResponseData DoRefreshToken(Parameters parameters, IRTokenRepository _repo, IOptions <Audience> _settings) { try { var token = _repo.GetToken(parameters.refresh_token, parameters.client_id).Result; if (token == null) { return(new ResponseData { Code = "905", Message = "can not refresh token", Data = null }); } if (token.IsStop == 1) { return(new ResponseData { Code = "906", Message = "refresh token has expired", Data = null }); } var refresh_token = Guid.NewGuid().ToString().Replace("-", ""); token.IsStop = 1; var updateFlag = _repo.ExpireToken(token).Result; var addFlag = _repo.AddToken(new RToken { ClientId = parameters.client_id, RefreshToken = refresh_token, Id = Guid.NewGuid().ToString(), IsStop = 0 }); if (updateFlag && addFlag.Result) { return(new ResponseData { Code = "999", Message = "OK", Data = GetJwt(parameters.client_id, refresh_token, _settings, BsonSerializer.Deserialize <RegisterModel>(MongoHelper.CheckForDatas("UserName", parameters.client_id, null, null, "Authentication", "Authentication")).UserRole) }); } else { return(new ResponseData { Code = "910", Message = "can not expire token or a new token", Data = null }); } } catch (Exception ex) { LoggerDataAccess.CreateLog("AuthHelper", "DoRefreshToken", ex.Message); return(new ResponseData { Code = "400", Message = "Failed", Data = null }); } }