Click here to Skip to main content
15,887,083 members
Home / Discussions / C#
   

C#

 
QuestionRe: Edit a WinForm on user mode Pin
Eddy Vluggen24-Jan-14 0:26
professionalEddy Vluggen24-Jan-14 0:26 
AnswerRe: Edit a WinForm on user mode Pin
BillWoodruff24-Jan-14 1:13
professionalBillWoodruff24-Jan-14 1:13 
QuestionDownload latest file from FTP using WinSCP in SSIS Pin
Rahul Vairagi23-Jan-14 18:16
Rahul Vairagi23-Jan-14 18:16 
AnswerRe: Download latest file from FTP using WinSCP in SSIS Pin
Pete O'Hanlon23-Jan-14 22:01
mvePete O'Hanlon23-Jan-14 22:01 
GeneralRe: Download latest file from FTP using WinSCP in SSIS Pin
Rahul Vairagi29-Jan-14 18:35
Rahul Vairagi29-Jan-14 18:35 
AnswerRe: Download latest file from FTP using WinSCP in SSIS Pin
Alan Balkany24-Jan-14 4:59
Alan Balkany24-Jan-14 4:59 
GeneralRe: Download latest file from FTP using WinSCP in SSIS Pin
Rahul Vairagi29-Jan-14 18:36
Rahul Vairagi29-Jan-14 18:36 
QuestionCalling MySql Function in C# for validating username and password Pin
ahmed_one23-Jan-14 17:58
ahmed_one23-Jan-14 17:58 
Development Environment:
Microsoft Visual Studio 2010 Ultimate,
C#,
MySql


Hi, I've create a function in mysql which accept 3 parameter to validate username and password.

SQL
DELIMITER $$

USE `generalledger`$$

DROP FUNCTION IF EXISTS `fLogin_Check`$$

CREATE DEFINER=`root`@`localhost` 
FUNCTION `fLogin_Check`
(mUserName   VARCHAR(50),mUserPass VARCHAR(40),mUserKey VARCHAR(40)) RETURNS INT
BEGIN
DECLARE mCount INT;

SELECT COUNT(*) INTO mCount FROM userMaster    
WHERE userName = mUserName
AND AES_DECRYPT(userPass, mUserKey) = UPPER( mUserPass);

IF mCount > 0 THEN
RETURN 1;
ELSE
RETURN  0;
END IF;
END$$

DELIMITER ;


As you can see I am using AES_DECRYPT function of MySql to check password, because I've use AES_ENCRYPT for password when INSERT username and password to mysql table.

Now I need to call the function fLogin_Check in C#, which I am doing by using following class method:

C#
public int CheckUser(string mUserName, string mPass, string mKey)
{
    oCn = da.GetConnection();

    int res;

    if (oCn == null)
    {
        oCn.Open();
    }

    sInsProcName = "fLogin_Check";
    insertcommand = new MySqlCommand(sInsProcName, oCn);
    insertcommand.CommandType = CommandType.StoredProcedure;
    insertcommand.Parameters.Add(new MySqlParameter("mRes", MySqlDbType.Int32, 0));
    insertcommand.Parameters["mRes"].Direction = ParameterDirection.ReturnValue;
    insertcommand.Parameters.Add("mUserName", MySqlDbType.VarChar, 50, mUserName);
    insertcommand.Parameters.Add("mUserPass", MySqlDbType.VarChar, 40, mPass);
    insertcommand.Parameters.Add("mUserKey", MySqlDbType.VarChar, 40);
    insertcommand.Parameters["mUserKey"].Value = mKey;

    res = insertcommand.ExecuteNonQuery();
    //res = int.Parse(insertcommand.Parameters["mRes"].Value.ToString());

    return (res);

    oCn.Close();
}


oCn is the connection abject which uses to call GetConnection method define in my DAL class and da is the object created from DAL class, use to opening and closing database connection.

Using following Global class I am storing username and password after user enter them, and then try to validating with fLogic_Check Mysql function:

C#
public static class Globals
{
    public static string userName;
    public static string userPass;
    public const string sKey = "AHMEDFINANCEICMAP1122";
}


sKey is the key I use to encrypt password when insert username. Now I am trying to use it in C# from Login Form when user enter Username and Password and click login button with following code:

C#
private void btnCheck_Click(object sender, EventArgs e)
{
    Globals.userName = txtUser.Text.ToString();
    Globals.userPass = txtPass.Text.ToString();

    if (fUser.CheckUser(Globals.userName, Globals.userPass, Globals.sKey) == 0)
    {
        MessageBox.Show("Invalid Username or Password.");
    }
    else
    {
        MessageBox.Show("Login Successfull");
    }
}


It always return 0, means failed login. I've checked the Mysql function in MySql GUI and it works fine:

SQL
SELECT fLogin_Check("AHMED","AHMED1981","AHMEDFINANCEICMAP1122") FROM userMaster


Which successfully return 1, however it fails when calling in C#. I've also tried to access Parameter which I've comment out after failure...What am I doing wrong?

Please guide..


Ahmed
AnswerRe: Calling MySql Function in C# for validating username and password Pin
Bernhard Hiller23-Jan-14 21:39
Bernhard Hiller23-Jan-14 21:39 
GeneralRe: Calling MySql Function in C# for validating username and password Pin
ahmed_one23-Jan-14 22:49
ahmed_one23-Jan-14 22:49 
SuggestionRe: Calling MySql Function in C# for validating username and password Pin
Richard Deeming24-Jan-14 1:56
mveRichard Deeming24-Jan-14 1:56 
GeneralRe: Calling MySql Function in C# for validating username and password Pin
Eddy Vluggen24-Jan-14 3:04
professionalEddy Vluggen24-Jan-14 3:04 
GeneralRe: Calling MySql Function in C# for validating username and password Pin
ahmed_one24-Jan-14 4:11
ahmed_one24-Jan-14 4:11 
GeneralRe: Calling MySql Function in C# for validating username and password Pin
Dave Kreskowiak24-Jan-14 4:37
mveDave Kreskowiak24-Jan-14 4:37 
GeneralRe: Calling MySql Function in C# for validating username and password Pin
Eddy Vluggen24-Jan-14 4:58
professionalEddy Vluggen24-Jan-14 4:58 
GeneralRe: Calling MySql Function in C# for validating username and password Pin
Dave Kreskowiak24-Jan-14 4:33
mveDave Kreskowiak24-Jan-14 4:33 
AnswerRe: Calling MySql Function in C# for validating username and password Pin
Shujaat Ullah Khan24-Jan-14 4:24
Shujaat Ullah Khan24-Jan-14 4:24 
QuestionValidation on characters having multiple section in a string Pin
nitin_ion23-Jan-14 16:47
nitin_ion23-Jan-14 16:47 
AnswerRe: Validation on characters having multiple section in a string Pin
Peter Leow23-Jan-14 17:20
professionalPeter Leow23-Jan-14 17:20 
GeneralRe: Validation on characters having multiple section in a string Pin
nitin_ion23-Jan-14 18:58
nitin_ion23-Jan-14 18:58 
GeneralRe: Validation on characters having multiple section in a string Pin
Dave Kreskowiak24-Jan-14 4:30
mveDave Kreskowiak24-Jan-14 4:30 
AnswerRe: Validation on characters having multiple section in a string Pin
Garth J Lancaster23-Jan-14 17:23
professionalGarth J Lancaster23-Jan-14 17:23 
Questionunicode characters Pin
Member 1037881923-Jan-14 4:26
professionalMember 1037881923-Jan-14 4:26 
AnswerRe: unicode characters Pin
Richard MacCutchan23-Jan-14 6:29
mveRichard MacCutchan23-Jan-14 6:29 
AnswerRe: unicode characters Pin
Bernhard Hiller23-Jan-14 21:48
Bernhard Hiller23-Jan-14 21:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.