Tuesday, August 26, 2014

DataAnnotation in models in edmx

In edmx model we have:
namespace DatabaseManager
{
    using System;
    using System.Collections.Generic;
   
    public partial class Item
    {

        public string IDescription { get; set; }
        public string IName { get; set; }

    }
}

If I set DataAnnotation like [Required] on property then it will not persist after edmx update. So it set annotation permanently we have to override meta information this class and merge our custom class with this class.
In our custom model:

namespace DatabaseManager
{
    [MetadataType(typeof(metaClass))]
    public partial class Item
    {

        private class metaClass
        {

            [DisplayName("Description")]
            public string IDescription { get; set; }

            [DisplayName("Name")]
            [Required()]
            public string IName { get; set; }

         }
     }
}

Now you set data annotation in your custom class it will work properly and no need to worry about edmx update.

Tuesday, August 12, 2014

Column level encryption in SQL server 2008r2

USE YourDB
GO

--get the list of keys in current adtabase
select * from sys.symmetric_keys

--get the list of certificates in current database
select * from sys.certificates


--To store encrypted data in the table you have to change the datatype to varbinary(256)


--set the database level encryption password
CREATE MASTER KEY ENCRYPTION
BY PASSWORD = '343k6WJussssszurWi'
GO

--create a certificate by which we are going to encrypt or decrypt data later
CREATE CERTIFICATE EncryptTESTCert
WITH SUBJECT = 'Encrypt
TESTCert'
GO

--create symmetric key by using certificate
CREATE SYMMETRIC KEY
TESTTableKey
WITH ALGORITHM = AES_256 ENCRYPTION
BY CERTIFICATE Encrypt
TESTCert
GO



--EXAMPLE

--Open encryption
OPEN SYMMETRIC KEY
TESTTableKey DECRYPTION
BY CERTIFICATE Encrypt
TESTCert

--encrypt data the show that
DECLARE @ResultVarBinary varbinary(256)              
SET @ResultVarBinary = ENCRYPTBYKEY(KEY_GUID('
TESTTableKey'),'test')  
select @ResultVarBinary

--decrypt data then show that
DECLARE @ResultSTring varchar(max)
SET @ResultSTring = CONVERT(VARCHAR(max),DECRYPTBYKEY(@ResultVarBinary))
select @ResultSTring

--close current encryption
CLOSE SYMMETRIC KEY
TESTTableKey


--NOTE
--Use ENCRYPTBYKEY while You are inserting data
--Use DECRYPTBYKEY When you are fetching data from table



Ref:
http://blog.sqlauthority.com/2009/04/28/sql-server-introduction-to-sql-server-encryption-and-symmetric-key-encryption-tutorial-with-script/


To use it in a easier way I have created separate SP and functions:-


ALTER procedure [dbo].[OpenEncryption]
AS
BEGIN
    OPEN SYMMETRIC KEY TESTTableKey DECRYPTION
    BY CERTIFICATE EncryptPCCASCert
END




ALTER procedure [dbo].[CloseEncryption]
AS
BEGIN
    CLOSE SYMMETRIC KEY
TESTTableKey
END




ALTER FUNCTION [dbo].[EncryptData]
(
    -- Add the parameters for the function here
    @text varchar(max)
)
RETURNS varbinary(256)
AS
BEGIN
    -- Declare the return variable here
    DECLARE @ResultVar varbinary(256)       
       
    SET @ResultVar = ENCRYPTBYKEY(KEY_GUID('TESTTableKey'),@text)   

    -- Return the result of the function
    RETURN @ResultVar

END




ALTER FUNCTION [dbo].[DecryptData]
(
    -- Add the parameters for the function here
    @text varbinary(256)
)
RETURNS varchar(max)
AS
BEGIN
    -- Declare the return variable here
    DECLARE @ResultVar varchar(max)       
       
    if @text is null
        begin
            SET @ResultVar =''
        end
    else
        begin
            SET @ResultVar = CONVERT(varchar(max),DECRYPTBYKEY(@text))
        end

    if @ResultVar is null
    BEGIN
        SET @ResultVar = ''
    END
    -- Return the result of the function
    RETURN @ResultVar

END


Sunday, August 3, 2014

ASP.Net 4.5 automatic minification for js and css

To reduce page content size I used asp.net 4.5 default minification system in my MVC 4 web application.
Step 1
 As per step 1 just use-
BundleTable.EnableOptimizations = true;
to minify all bundled js and css with no effort.

Step 2

As per step 2 -
To make it functional set debug="false" in web.config file.