Choose your development style for managed code – Authenticate using code
From MSDN Web Site i took this few examples for authentication against CRM:
Description and usage
The classes in the sample code demonstrate how to connect to the web services and authenticate the user. You can use the helper code as a basis of your own custom authentication code.
This code is easy to use and supports all Microsoft Dynamics CRM deployment types. It also supports storing users’ passwords in the Windows Credential Manager for later reuse.
Full source code is provided so you can copy and customize it for your needs. This is a recommended practice to isolate your programs from changes to this helper code in future releases.
Sample: Quick start for Microsoft Dynamics CRM
Use the sample and helper code
Helper code: ServerConnection class
This code can be found in the SampleCodeCSHelperCodeCrmServiceHelpers.cs and SampleCodeVBHelperCodeCrmServiceHelpers.vb files in the SDK. Download the Microsoft Dynamics CRM SDK package.
These assemblies are provided to simplify and accelerate the development of applications that interact with Microsoft Dynamics CRM. The extensions extend the functionality of the core Microsoft Dynamics CRM SDK specifically around the use of the OrganizationServiceContext class.
For an easy method that does the hard work for you in a few lines of code, use the CrmConnection class.
This code is easy to use and supports all Microsoft Dynamics CRM deployment types.
Developer extensions for Microsoft Dynamics CRM
Simplified connection to Microsoft Dynamics CRM
For advanced developers who need to customize the Windows Communication Foundation (WCF) service channel management and the authentication process, use the IServiceManagement and OrganizationServiceProxy classes in the Microsoft.Xrm.Sdk.Client namespace.
Using these classes directly can provide better connection and authentication performance, and more flexibility. However, they require more advanced knowledge of the WCF service channel and server authentication. In addition, you must write more code to handle all Microsoft Dynamics CRM deployment types.
Active Directory and claims-based authentication
Sample: Authenticate users with Microsoft Dynamics CRM web services
Improve Service Channel Allocation Performance
These assemblies leverage the CRM APIs to provide easy authentication support with fewer lines of code and through Windows PowerShell cmdlets. All the function calls in these classes provide thread-safety for actions performed in CRM in a multithreaded environment. It provides a common sign-in control with integrated authentication logic and an ability to securely store and reuse the authentication information to provide a consistent and seamless sign-in experience to CRM from your Windows client applications.
These classes also provide built-in diagnostic tracing to aid troubleshooting and performance reporting of the action calls from your Windows client applications.
These classes support all Microsoft Dynamics CRM deployment types and authentication types, except OAuth.
Build Windows client applications using the XRM tools
Use XRM tooling to connect to CRM
Use PowerShell cmdlets for XRM tooling to connect to CRM
Sample: Quick start for XRM Tooling API
Hope it helps.