コード例 #1
0
        private string GenerateQRCode(MTwoFactAuthencation param)
        {
            string strQRCode        = string.Empty;
            string AuthenticatorUri = GenerateQrCodeUri(param.UserName, param.Secret);

            clsResponse objResponseMongo = QRCode(AuthenticatorUri);

            if (objResponseMongo.StatusCode == 200)
            {
                strQRCode = objResponseMongo.Base64String;
            }
            else
            {
                strQRCode = objResponseMongo.StatusText;
            }
            return(strQRCode);
        }
コード例 #2
0
        private clsResponse SaveinMongo(MTwoFactAuthencation param, clsResponse prmResponse)
        {
            clsResponse objResponse = new clsResponse();

            try
            {
                var colPurge = mongoDatabase.GetCollection <BsonDocument>(strMongoDBTableName);
                var docPurge = new BsonDocument
                {
                    { "UserName", param.UserName }, { "Secret", prmResponse.TwoFactorSecret }, { "Status", 0 }
                };
                colPurge.Insert(docPurge);

                objResponse.StatusCode = 200;
            }
            catch (Exception ex)
            {
                objResponse.StatusCode = 404;
                objResponse.StatusText = ex.Message.ToString();
            }
            return(objResponse);
        }
コード例 #3
0
        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);
        }