Amazon Ad

Wednesday, 26 November 2014

Generate Password As Per Password Policy in C#

Hi Guys,

I was given a task to generate a password having following conditions

1. Min 10 Chracters
2. Must have one upper case letters
3. Must have one or more lower case letters
4. Must have one or more special characters
5. Must have one or more numbers.

Following is how i was able to do the same in C#

private string ActionNewPassword()
    {
        String Num = "0123456789";
        String Sp = "!@#$%&*()+{}[]^-";
        String SL = "abcdefghijklmnopqrstuvwxyz";
        String UCL = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        Random rnd = new Random();
       
        //One or more upper case letters
        string part1 = UCL.Substring(rnd.Next(UCL.Length),1);

        //One or more numbers
        string part2 = Num.Substring(rnd.Next(Num.Length), 1);

        //Minimum 4 small case letters
        string part3 = "";
        for(int i=0;i<4;i++)
            part3 += SL.Substring(rnd.Next(SL.Length), 1);

        //Minimum 1 special character
        string part4 = Sp.Substring(rnd.Next(Sp.Length), 1);

        //Minimum 3 small numbers
        string part5 = "";
        for (int i = 0; i < 3; i++)
            part5 += SL.Substring(rnd.Next(SL.Length), 1);

        string pwd = part1 + part2+part3+part4+part5;

        //Message.InnerHtml = pwd;
       
        return pwd;
    }

Thanks
Ritesh Tandon

Generate Verification Code In MS SQL

Hi Guys,

I was given a task to generate an email verification code from MS SQL. I finally achieved with some  string functions and inbuilt Random function in MS SQL. Here is the code

    --Generate verification code
    declare @ab varchar(36)='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
    declare @vercode varchar(11)=''
    declare @counter int=0
    declare @ind int=0
    WHILE @counter < 9
    BEGIN
        set @ind=(select RAND()*35)
        set @vercode=@vercode+(select SUBSTRING(@ab, @ind+1, 1))
        SET @counter = @counter + 1
    END
    print @vercode

The below code generates an alphanumeric code having 9 alphanumeric characters. You can increase or decrease the length by chaning the lenght of the @vercode variable and the while loop counter.

Thanks
Ritesh

Friday, 29 August 2014

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.BadImageFormatException: Invalid access to memory location. (Exception from HRESULT: 0x800703E6)

Dear all,

I was struggling with an error in ASP.NET. The error was coming when i implemented Ad (Active Directory) password change code in one of my forms. It some times worked but sometimes it gave the following error

 System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.BadImageFormatException: Invalid access to memory location. (Exception from HRESULT: 0x800703E6) --- End of inner exception stack trace --- at System.DirectoryServices.DirectoryEntry.Invoke(String methodName, Object[] args) at _adPassword.ChangeUserADPassword(String Username, String Password, String newPwd)

I searched a lot and then found a solution for this problem. The problem was the CPU overload on the server. The server CPU usage when moved above 75-90% this form resulted this error. I recycled the Application pool and also made some changes on the server to maintain the CPU usage below 75%. This solved the problem and now i don't face any sort of problem with this form.

Thanks
Ritesh Tandon

Saturday, 12 July 2014

Creating Fault Injections For ASP.NET MVC Web API

Hi Guys,

I was working on a new project which was having a challenge to create fault injection in a running Web API.

Step 1 : Create a model Class name "User"

public class User
    {
        public int Id { get; set; }
        public string Email { get; set; }
        public string Password { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Gender { get; set; }
    }


Wednesday, 9 July 2014

Merge Git Branch

Hi guys,

Today i am going to tell you the commands to merge your branch with your live branch. Assuming that you have two branches a. Test b. Live. Where Test branch has all the development files where as "Live" branch has the data of the live website. Here is how we can merge these two branches.

1. git pull origin test
2. git checkout live (in case the branch is not in local rep. then use git checkout -b live)
3. git branch (To check which branch is current branch. The live branch will be your current branch)
4. git add .
5. git commit -m "message"
6. git status
7. git merge test (Merge the live branch with test branch)
8. git pull origin live
9. git push origin live
10. git pull origin live

In case you want to pull all the live contents on to your local repository :
git reset --hard origin/test
This tells it to fetch the commits from the remote repository, and position your working copy to the tip of its master branch. The above command will replace all the local files from "test" branch with all the files on the Github remote branch "test".

Thanks
Ritesh

Thursday, 3 July 2014

Selectize.js Dependent Dropdowns For Country And City

Hi Guys,

I was working with selectize.js and a problem occurred when i was using select/dropdown list, I was working in PhP. Here is the code for country dropdown which successfully loads city on selecting country from country dropdown.

<div class="form-group mb10">

        <div class="row">

            <div class="col-sm-6 mb5">

                <label class="control-label">Country</label>

                <div class="has-icon pull-right">

                    <?php echo CHtml::dropDownList('ClientProjects[country]','',CHtml::listData(States::model()->findAll(array('order'=>'name ASC')),'id', 'name'),array('class'=>"form-control pr10",'prompt' =>'Select Country','id'=>"country",'ajax'=>array(

'type'=>'POST',

'url' => CController::createUrl('/globaldata/getCity'),

'data'=> array('country'=>'js:this.value'),

'success'=>'function(data){loadcity1(data);}'

)

));?>

                 </div>

            </div>

How to avoid reentering of github user and password when pull or push

Hi Guys,

I was facing a problem when i was working on a project on github. The pull and push git commands were asking me my github username and password when i pushed or pulled the branch. Following are the steps :

Step 1 : Remove the already added origin if any by using this command

>git remote rm origin

Step 2 : Now we will add an origin with username and password, This will get stored in the git file and won't ask the username and password again.

>git remote add origin https://username:password@github.com/organizationname/repositorypath

where username is your github user name and password is the password of your github user. In case you cannot find the organizationname and repositorypath, You can find this by copying the https clone URL of the repository. i.e incase your your URL is "https://github.com/riteshtandon23/MyCodio.git" having username as "test" and password as "test123" then your git command will be


>git remote add origin https://test:test123@github.com/riteshtandon23/MyCodio.git

Step 3 : Give a command to pull the branch on local

>git pull origin master

It won't ask you any login or password same will happen with the push command.

> git push origin master

Hope it helped you.a

Thanks
Ritesh

Monday, 26 May 2014

jQuery ajax POST and saving value in local javascript variable


Hi Guys,

I was working on an ASP.NET page and found a strange problem. Whenever i
was calling the jquery ajax POST request, The data from webservice was
available in the success function but not after $.ajax request code. This
was strange i as wanted to access the data after calling the ajax POST
request.

Here is the problem

var localdata;
$.ajax({
data:"{'id':'1'}",
type:"POST",
dataType:"json",
contentType:"application/json",
url:"http://www.test.com/test.svc/HelloWorld",
success:function(data){localdata=data;alert("Inside Success "+localdata);},
error:function(a,b,c){}
});
alert("After ajax "+localdata);

After executing the above it gave me "Inside Success Hello", But the second
alert message gave me "After ajax undefined". This was due to the fact
that the first call was made inside success which was asynchronous, Whereas
the second alert was not asynchronous and didn't wait until the
$.ajax POST request executed.

To solve the same, I made it synchronous as i had to store the result of
webservice in a local variable, Here is the code

var localdata;
$.ajax({
data:"{'id':'1'}",
type:"POST",
dataType:"json",
        async:false, //setting this made it a synchronous call
contentType:"application/json",
url:"http://www.test.com/test.svc/HelloWorld",
success:function(data){localdata=data;alert("Inside Success "+localdata);},
error:function(a,b,c){}
});
alert("After ajax "+localdata);

Thanks
Ritesh Tandon

Thursday, 1 May 2014

Creating Asynchronous Thread in Global.asax in MVC

Hi guys,

I was facing a problem of creating asynchronous thread in Global.asax in MVC. I was able to create a thread which runs asynchrounsly without affecting the application. Here is how how i did it in global.asax.cs file. It also requires two namespaces 1. System.Threading 2. System.Threading.Tasks, Please do add them.

Step 1: Create a async Task, here in this example its MyThread.

private async Task<int> MyThread()
        {
            while (true)
            {
                string result = await Operations();
                await Task.Delay(5000);
            }
            return 1;
        }

Step 2: Create another async Task performing all the operations.

public async Task<String> Operations()
        {
            return "Hello";
        }
Step 3: Inside Application_Start call the thread.

protected async void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            AllSync();
            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            AuthConfig.RegisterAuth();
        }

Thanks
Ritesh Tandon

Monday, 21 April 2014

Creating Dependent DropDownList in MVC without jQuery.



Hi Guys,

I was given a task to generate dependent values in DropDownList without using jQuery. However this approach is not recommended but incase you still want to generate the dependent dropdownlist values based upon the parent dropdownlist value, Here are the steps to do so.

In my example i am using an example of Category, SubCategory and Product. Based on the category dropdown, subcategory dropdown list is generated and the product is then saved based on the category and subcategory values. In this example i have taken both category and subcategory reference in product table, However it can be done by taking only reference of subcategory table.

Step 1: Generate models
public class Category
    {
        [Key]
        public virtual int CategoryId { get; set; }
        public virtual string CategoryName { get; set; }
        public List<SubCategory> SubCategories { get; set; }
        public List<Product> Products { get; set; }
    }

Wednesday, 16 April 2014

How to get client's MAC Address from browser


Hi Guys,

I was having problem of finding out MAC address from browser. I finally achieved the same by using help from a blog and jQuery. The current code is tested on local LAN and it gives the MAC address based on the local LAN ip address.

Here is how i achieved it


How to implement Captcha v3 in ASP.NET

 I was facing an issue of dom parsing in my website. I finally resolved it by using Google Captcha V3. Step 1: Get your keys from https:...