Friday 31 October 2014

Generate password and shuffle the characters

In a recent project I had to create a function to generate a password with an exact number alpha characters, numeric and symbols and then shuffle them.

numOfNumericChars (Indicates number of numeric characters);

numOfAlphaChars (Indicates number of alpha characters);

numOfSynbolChars ( Indicates number of symbol characters)

validNumChars (Valid characters for numeric. Default is all characters from 0- 9)

validAlphaChars (Valid characters for alpha. Default is all characters from a- z  and A-Z)

validSymbolChars (Valid character for symbols. Default is all characters in  !-+_@&$#%)

C#
public string generatePassword(int numOfNumericChars, int numOfAlphaChars, int numOfSynbolChars, string validNumChars = "1234567890", string validAlphaChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", string validSymbolChars = "!-+_@&$#%")  
     {  
       StringBuilder res = new StringBuilder();  
       Random rnd = new Random();  
       while (0 < numOfNumericChars--)  
       {  
         res.Append(validNumChars[rnd.Next(validNumChars.Length)]);  
       }  
       while (0 < numOfAlphaChars--)  
       {  
         res.Append(validAlphaChars[rnd.Next(validAlphaChars.Length)]);  
       }  
       while (0 < numOfSynbolChars--)  
       {  
         res.Append(validSymbolChars[rnd.Next(validSymbolChars.Length)]);  
       }  
       string stgPassword = res.ToString();  
       //Shuffle password  
       char[] array = stgPassword.ToCharArray();  
       Random rng = new Random();  
       int n = array.Length;  
       while (n > 1)  
       {  
         n--;  
         int k = rng.Next(n + 1);  
         var value = array[k];  
         array[k] = array[n];  
         array[n] = value;  
       }  
       return stgPassword = new string(array);  
     }  


Thursday 30 October 2014

IOrganizationService

What is IOrganizationService?

Is the web service that accesses data and metadata for your organization. 

Why do I need a IOrganizationService?

You need IOrganizationService when you want to work with data in our Microsoft Dynamics CRM system. The methods available are:
  • -          Create (Create a record of an entity)
  • -          Retrieve (Retrieve a record of an entity)
  • -          RetrieveMultiple ( Retrieve a collection of records of an entity. This can be used with query expression or fetch XML)
  • -          Update (Update a record of an entity)
  • -          Delete (Delete a record of an entity)
  • -          Associate (Create a link between two records)
  • -          Disassociate (Delete a link between two records)
  • -          Execute (Execute a process. The must common is workflows, imports and detect duplicates)


How can I get IOrganizationService?

To get the organization service you need 3 thing:
  • -          User (username of a valid user on the CRM organization)
  • -          Password (valid password for the user)
  • -          Uri (You can get the organization service Uri in your Dynamics CRM system. Go to Settings à Customizations à Developer Resources.)


How can I use it on code?

Need a reference to the microsoft.xrm.sdk.dll from the SDK

C#
//organization url  
 IServiceManagement<IOrganizationService> orgServiceManagement = ServiceConfigurationFactory.CreateManagement<IOrganizationService>(new Uri(“Uri”));  
 //Put Credentials  
       AuthenticationCredentials authCredentials = new AuthenticationCredentials();  
       authCredentials.ClientCredentials.UserName.UserName = “user”;  
       authCredentials.ClientCredentials.UserName.Password = “password”;  
 AuthenticationCredentials tokenCredentials = orgServiceManagement.Authenticate(authCredentials);  
 //connection to CRM  
 OrganizationServiceProxy Service = new OrganizationServiceProxy(orgServiceManagement, tokenCredentials.SecurityTokenResponse);  
       Service.EnableProxyTypes();  
       IOrganizationService _service = Service;  

First Post


The main idea for this blog is to help others but also take notes for future projects.

I'll triy to insert posts regularly so this blog is updated.