public clsReturn Renew(string UserName) { clsReturn objResponse = new clsReturn(); try { string strGeneratedCodeStatusNotZero = mongoDatabase.GetCollection <MTwoFactAuthencation>(strMongoDBTableName).FindAll().Where(t => t.UserName.ToLower() == UserName.ToLower()).Select(t => t.Secret).FirstOrDefault(); if (strGeneratedCodeStatusNotZero == null || strGeneratedCodeStatusNotZero == "") { objResponse.StatusCode = 404; objResponse.Response = "failed"; return(objResponse); } var collection = mongoDatabase.GetCollection <BsonDocument>(strMongoDBTableName); var query = Query <MTwoFactAuthencation> .EQ(e => e.UserName, UserName); collection.Remove(query); objResponse.StatusCode = 200; objResponse.Response = "success"; } catch (Exception ex) { objResponse.StatusCode = 404; objResponse.Response = "failed"; objResponse.StatusText = ex.Message.ToString(); } return(objResponse); }
public clsReturn VerifyCode(string UserName, string strAppCode) { clsReturn objResponse = new clsReturn(); try { string strResponse = string.Empty; string strGeneratedCodeStatusNotZero = mongoDatabase.GetCollection <MTwoFactAuthencation>(strMongoDBTableName).FindAll().Where(t => t.UserName.ToLower() == UserName.ToLower() && t.Status != 0).Select(t => t.Secret).FirstOrDefault(); if (strGeneratedCodeStatusNotZero == null || strGeneratedCodeStatusNotZero == "") { string strGeneratedCodeStatusZero = mongoDatabase.GetCollection <MTwoFactAuthencation>(strMongoDBTableName).FindAll().Where(t => t.UserName.ToLower() == UserName.ToLower() && t.Status == 0).Select(t => t.Secret).FirstOrDefault(); if (TimeBasedOneTimePassword.IsValid(strGeneratedCodeStatusZero, strAppCode)) { strResponse = "success"; var collection = mongoDatabase.GetCollection <BsonDocument>(strMongoDBTableName); var query = Query <MTwoFactAuthencation> .EQ(e => e.UserName, UserName); var update = Update <MTwoFactAuthencation> .Set(e => e.Status, 1); collection.Update(query, update); } else { strResponse = "failed"; var collection = mongoDatabase.GetCollection <BsonDocument>(strMongoDBTableName); var query = Query <MTwoFactAuthencation> .EQ(e => e.UserName, UserName); collection.Remove(query); } } else { if (TimeBasedOneTimePassword.IsValid(strGeneratedCodeStatusNotZero, strAppCode)) { strResponse = "success"; } else { strResponse = "failed"; } } objResponse.StatusCode = 200; objResponse.Response = strResponse; } catch (Exception ex) { objResponse.StatusCode = 404; objResponse.StatusText = ex.Message.ToString(); } string strJson = JsonConvert.SerializeObject(objResponse); return(objResponse); }
public clsReturn Is2FAEnable(string UserName) { clsReturn objResponseMain = new clsReturn(); try { var _success = false; var colPurge = mongoDatabase.GetCollection <BsonDocument>(strMongoDBTableName); var Detail = mongoDatabase.GetCollection <MTwoFactAuthencation>(strMongoDBTableName).FindAll().Where(t => t.UserName.ToLower() == UserName.ToLower()); string strSecret = Detail.Select(t => t.Secret).FirstOrDefault(); int intStatus = Detail.Select(t => t.Status).FirstOrDefault(); if (strSecret == null || strSecret == "") { _success = false; } else { _success = true; } if (_success == false) { clsResponse objResponseGenSecret = GenerateSecret(16); if (objResponseGenSecret.StatusCode != 200) { return(objResponseMain); } MTwoFactAuthencation objMTwoFactAuthencation = new MTwoFactAuthencation(); objMTwoFactAuthencation.UserName = UserName; objMTwoFactAuthencation.Secret = objResponseGenSecret.EncodedKey; objResponseMain.Response = GenerateQRCode(objMTwoFactAuthencation); clsResponse objResponseMongo = SaveinMongo(objMTwoFactAuthencation, objResponseGenSecret); if (objResponseMongo.StatusCode != 200) { return(objResponseMain); } } else { if (intStatus == 0) { MTwoFactAuthencation objMTwoFactAuthencation = new MTwoFactAuthencation(); objMTwoFactAuthencation.UserName = UserName; objMTwoFactAuthencation.Secret = new Base32Encoder().Encode(Encoding.ASCII.GetBytes(strSecret)); objResponseMain.Response = GenerateQRCode(objMTwoFactAuthencation); } } objResponseMain.StatusCode = 200; } catch (Exception ex) { objResponseMain.StatusCode = 404; objResponseMain.StatusText = ex.Message.ToString(); } string strJson = JsonConvert.SerializeObject(objResponseMain); return(objResponseMain); }