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.

Friday, July 25, 2014

Getting the left section of current Url upto domain name in C#

I was creating product's full links dynamically to do this I need to have the left section of the current URL from http to end of domain name.

Example:

My full link:
http://pro-post.blogspot.in/2014/07/extended-dropdownlistfor-to-create.html

I need: 
http://pro-post.blogspot.in

To achieve this ->

string leftSection = System.Web.HttpContext.Current.Request.Url.OriginalString.Replace(
System.Web.HttpContext.Current.Request.Url.PathAndQuery, "")
leftSection = leftSection.Substring(0, leftSection.LastIndexOf(':'));

ref: Soumyadip [http://soumyadip-cooldips.blogspot.in]

Wednesday, July 16, 2014

Extended DropdownListFor to create colored options

I had a situation to create a drop down list with multicolored options according to some criteria.
Like a dropdown consist of users, suppose users who are Administrator they will be shown in Green, if Restricted User then Red.
I have created this with DropDownListFor, by which it can be implemented easyly in mvc applications.

Steps:
1. Install Nuget package installer in visual studio.
2. Open Nuget Package Console
3. Write- Install-Package DropDownWithColorLegend

Friday, June 27, 2014

Show a perticular element at top of view port using jquery


Requirement:
 Show a particular section on load of the page. only when query string present in URL
 for that page and where div id- showsection exist.


Solution:
  if(document.getElementById("showsection") != null) 
  {
    var str = document.URL.split('?')[1];
    if(str != undefined && str.length>0)
    {
      jQuery("html, body").animate({scrollTop:jQuery("#showsection").offset().top},1000);
    }
  }


In HTML page:
<div id="showsection"></div>
<div id="test">
    test document
</div>

Description:
By placing javascript code in document ready, whole page will be scrolled up and show "test".
You do not have to put anything inside "showsection" div

Monday, June 2, 2014

datagrid - Best Grid Control for ASP.NET MVC

different types of MVC grids

webgrid:
http://jasminewisp.blogspot.in/2013/11/webgrid-in-mvc-3-with-server-side.html
http://www.codeproject.com/Tips/615776/WebGrid-in-ASP-NET-MVC

jqtable
http://www.jtable.org/
http://www.codeproject.com/Articles/277576/AJAX-based-CRUD-tables-using-ASP-NET-MVC-3-and-jTa

grid mvc
http://gridmvc.codeplex.com/

asp.net awesome grid
http://demo.aspnetawesome.com/GridDemo/Grouping

ref:
http://stackoverflow.com/questions/20543265/what-is-the-best-grid-control-for-asp-net-mvc-telerik-devexpress-or-syncfusion
http://newskona.com/5-grid-extension-controls-for-asp-net-web-mvc-application/


Please suggest if you got some more info.