2
Answers

How to add member details to the database table using webAPI

Sivakumar

Sivakumar

9y
409
1
Hi,
 
I want to add member details to the table 
This is my Tables structure :
 
 
 
I am writing stored procedure like this :
 
GO
/****** Object: StoredProcedure [dbo].[uspAddMember] Script Date: 9/25/2015 11:43:56 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[uspAddMember]
@MemberName NVARCHAR(50),
@LoginName NVARCHAR(50),
@LoginPassword NVARCHAR(100)
AS BEGIN

SET NOCOUNT ON;

DECLARE @MemberID INT;

INSERT INTO Member(MemberName, LoginPassword)
VALUES(@MemberName, @LoginName);

SET @MemberID = SCOPE_IDENTITY();

INSERT INTO MemberLogin(MemberID, MemberLoginName)
VALUES(@MemberID, @LoginName);

RETURN @MemberID;

END
 
I want to add member details with hash password using web api.
 
This is code i written in BusinessLogic 
 
namespace Kiwi.Service.BL
{
public class MemberManager
{
private static HashAlgorithm hashAlgorithm = HashAlgorithm.Create("SHA256");
private DataAccess _kiwidb = new DataAccess();

public void CreateMember(string memberLoginName, string loginPassword)
{
string hashedPassword = HashPassword(loginPassword, null, hashAlgorithm);
} 
 
#region Hash Algorithm
private bool VerifyHashedPassword(string password, string storedPassword)
{
int saltLength = SaltValueSize * UnicodeEncoding.CharSize;

if (string.IsNullOrEmpty(storedPassword) ||
string.IsNullOrEmpty(password) ||
storedPassword.Length < saltLength)
{
return false;
}

// Strip the salt value off the front of the stored password.
string saltValue = storedPassword.Substring(0, saltLength);
string hashedPassword = HashPassword(password, saltValue, hashAlgorithm);
return storedPassword.Equals(hashedPassword, StringComparison.Ordinal);
}

private static string GenerateSaltValue(int saltValueSize)
{
UnicodeEncoding utf16 = new UnicodeEncoding();
if (utf16 == null)
return null;

// Create a random number object seeded from the value
// of the last random seed value. This is done
// interlocked because it is a static value and we want
// it to roll forward safely.

Random random = new Random(unchecked((int)DateTime.Now.Ticks));
if (random == null)
return null;

// Create an array of random values.
byte[] saltValue = new byte[saltValueSize];

random.NextBytes(saltValue);

// Convert the salt value to a string. Note that the resulting string
// will still be an array of binary values and not a printable string.
// Also it does not convert each byte to a double byte.

string saltValueString = utf16.GetString(saltValue);

// Return the salt value as a string.
return saltValueString;

}

private const int SaltValueSize = 4;
private static string HashPassword(string clearData, string saltValue, HashAlgorithm hash)
{
UnicodeEncoding encoding = new UnicodeEncoding();

if (string.IsNullOrEmpty(clearData) == true
|| hash == null || encoding == null)
return null;


// If the salt string is null or the length is invalid then
// create a new valid salt value.

if (saltValue == null)
{
// Generate a salt string.
saltValue = GenerateSaltValue(SaltValueSize);
}

// Convert the salt string and the password string to a single
// array of bytes. Note that the password string is Unicode and
// therefore may or may not have a zero in every other byte.

byte[] binarySaltValue = new byte[SaltValueSize];

binarySaltValue[0] = byte.Parse(saltValue.Substring(0, 2), System.Globalization.NumberStyles.HexNumber, CultureInfo.InvariantCulture.NumberFormat);
binarySaltValue[1] = byte.Parse(saltValue.Substring(2, 2), System.Globalization.NumberStyles.HexNumber, CultureInfo.InvariantCulture.NumberFormat);
binarySaltValue[2] = byte.Parse(saltValue.Substring(4, 2), System.Globalization.NumberStyles.HexNumber, CultureInfo.InvariantCulture.NumberFormat);
binarySaltValue[3] = byte.Parse(saltValue.Substring(6, 2), System.Globalization.NumberStyles.HexNumber, CultureInfo.InvariantCulture.NumberFormat);

byte[] valueToHash = new byte[SaltValueSize + encoding.GetByteCount(clearData)];
byte[] binaryPassword = encoding.GetBytes(clearData);

// Copy the salt value and the password to the hash buffer.

binarySaltValue.CopyTo(valueToHash, 0);
binaryPassword.CopyTo(valueToHash, SaltValueSize);

byte[] hashValue = hash.ComputeHash(valueToHash);

// The hashed password is the salt plus the hash value (as a string).

string hashedPassword = saltValue;

foreach (byte hexdigit in hashValue)
{
hashedPassword += hexdigit.ToString("X2", CultureInfo.InvariantCulture.NumberFormat);
}

// Return the hashed password as a string.

return hashedPassword;
}
#endregion

}
}

 
 
How to implement Dataaccess layer class and Web api controller.
 
Please give me code for that.
Answers (2)