private async Task <Users> Authenticate(LoginModel login) { try { var client = MongoDBClient.Connect(); var database = client.GetDatabase("netCoreAPI"); var collection = database.GetCollection <Users>("users"); var document = await collection.Find(u => u.Username.Equals(login.Username) && u.Password.Equals(login.Password)).FirstOrDefaultAsync(); //Console.WriteLine(document.ToString()); return(document); } catch (Exception exc) { _logger.LogError(exc, "Authenticate"); return(null); } #region Manual Authentication //Users user = null; //// Full Access Admin Example //if (login.Username == "admin" && login.Password == "admin") //{ // user = new Users { name = "Admin M", email = "*****@*****.**", birthdate = DateTime.Parse("Jan 1, 1981") }; //} //if (login.Username == "mohsen" && login.Password == "secret") //{ // //user = await _userManager.FindByEmailAsync(model.Email); // user = new Users { name = "Mohsen M", email = "*****@*****.**" }; //} #endregion }
public async Task <IActionResult> CheckUserAsync([FromBody] LoginModel login) { if (ModelState.IsValid) { try { var client = MongoDBClient.Connect(); var database = client.GetDatabase("netCoreAPI"); var collection = database.GetCollection <BsonDocument>("users"); var count = collection.CountDocuments(new BsonDocument()); // we don’t need all the data contained in a document. var projection = Builders <BsonDocument> .Projection.Exclude("_id"); var document = await collection.Find(new BsonDocument()).Project(projection).FirstOrDefaultAsync(); //Console.WriteLine(document.ToString()); _logger.LogInformation("CheckUserAsync \n" + document.ToString()); //var doc = document.ToJson(typeof(Users)); var json = Json(document.ToString()); // var j = Json(doc); if (document != null) // result.Succeeded { //var user = Json(document); //var jsonWriterSettings = new MongoDB.Bson.IO.JsonWriterSettings { OutputMode = MongoDB.Bson.IO.JsonOutputMode.Strict }; //var user1 = document.ToJson(jsonWriterSettings); object val = BsonTypeMapper.MapToDotNetValue(document); string jsonString = JsonConvert.SerializeObject(val); var user = JsonConvert.DeserializeObject <Users>(jsonString); return(Ok(new { user })); } return(Unauthorized()); } catch (Exception exc) { _logger.LogError(exc, "CheckUserAsync"); return(Json(exc.Message)); } } return(BadRequest(ModelState)); }