Authenticate SharePoint with ASP.NET/C#

Today I am going to explain how to use SharePoint online using office 360 in ASP.Net.

To use SharePoint document or any other information ,data in asp.net you'll authenticate from SharePoint online server following these step.
 
Step 1 - Use NameSpace "using Microsoft.SharePoint.Client;" after adding reference in your application.
 
 PM> Install-Package Microsoft.SharePoint.Client.dll 
 
Step 2 - Write code to authenticate from SharePoint in asp.net.
  1.                 string userName = Your username;  
  2.                 string password = Your password;  
  3.                 string baseurl = "Sharepoint base URL";  
  4.                 Uri uri = new Uri(baseurl);  
  5.                 var securePassword = new SecureString();  
  6.                 foreach (var c in password) { securePassword.AppendChar(c); }  
  7.                 var credentials = new SharePointOnlineCredentials(userName, securePassword);  
  8.                 var authCookie = credentials.GetAuthenticationCookie(uri);  
  9.                 var fedAuthString = authCookie.TrimStart("SPOIDCRL=".ToCharArray());  
  10.                 if (fedAuthString != null)  
  11.                 {  
  12.                     result.UserToken = fedAuthString;  
  13.                     result.Message = "User has been logged in successfully";  
  14.                 }  
  15.                 else  
  16.                 {  
  17.                     result.Message = "Invalid User!!";  
  18.   
  19.                 }  
This code use to authenticate with SharePoint in asp.net web application, Web API, MVC application.
 
After login you can get any information from SharePoint server which is authorized for this user.