/// <summary> /// Authenticate User from Active directory based on his email address and password /// </summary> /// <param name="emailAddress">Email Address</param> /// <param name="password">Password</param> /// <returns>Session</returns> public Session AuthenticateUserUsingCredentials( AuthDataRequest authData ) { UserInfoResponse userInfo = new UserInfoResponse(); string emailAddress = authData.username; string password = authData.password; Session stat = new Session(); string msg = string.Empty; if ( string.IsNullOrEmpty( emailAddress ) || string.IsNullOrEmpty( password ) ) { stat.Message = "Email and/or password can't be empty!"; stat.IsAuthenticated = false; return stat; } try { userInfo = GetUserAttributes( emailAddress ); if ( userInfo == null ) { stat.Message = "Error: Couldn't fetch user information!"; stat.IsAuthenticated = false; return stat; } var directoryEntry = new DirectoryEntry( LocalGcUri , userInfo.Upn , password ); directoryEntry.AuthenticationType = AuthenticationTypes.None; var localFilter = string.Format( AdSearchFilter , emailAddress ); var localSearcher = new DirectorySearcher( directoryEntry ); localSearcher.PropertiesToLoad.Add( "mail" ); localSearcher.Filter = localFilter; var result = localSearcher.FindOne(); if ( result != null ) { stat.Message = "You have logged in successfully!"; stat.IsAuthenticated = true; //Set the session Data SessionData session = new SessionData(); session.Username = userInfo.EmailAddress; session.Password = password; session.SessionStart = DateTime.Now; //Encrypt Session Data stat.SessionKey = SessionHandler.EncryptSession( session ); return stat; } stat.Message = "Login failed, please try again."; stat.IsAuthenticated = false; return stat; } catch ( Exception ex ) { stat.Message = "Wrong Email and/or Password " + ex; stat.IsAuthenticated = false; return stat; } }
/// <summary> /// Get user attrubutes from email address /// </summary> /// <param name="mailAddress">User Email Address</param> /// <returns>UserInfo Object</returns> public UserInfoResponse GetUserAttributes( RequestBody request ) { var userInfo = new UserInfoResponse(); string sessionKey = request.SessionKey; string mailAddress = request.Body; Session stat = ValidateSession( sessionKey ); if ( stat.IsAuthenticated == true ) { var searchFilter = string.Format( AdSearchFilter , mailAddress ); searcher.Filter = searchFilter; try { var searchResult = searcher.FindOne(); if ( searchResult != null ) { if ( searchResult.Properties.Contains( "title" ) ) userInfo.Title = (string)searchResult.Properties[ "title" ][ 0 ]; if ( searchResult.Properties.Contains( "givenName" ) ) userInfo.FirstName = (string)searchResult.Properties[ "givenName" ][ 0 ]; if ( searchResult.Properties.Contains( "sn" ) ) userInfo.LastName = (string)searchResult.Properties[ "sn" ][ 0 ]; if ( searchResult.Properties.Contains( "cn" ) ) userInfo.DisplayName = (string)searchResult.Properties[ "cn" ][ 0 ]; if ( searchResult.Properties.Contains( "samaccountname" ) ) userInfo.SamAccountName = (string)searchResult.Properties[ "samaccountname" ][ 0 ]; if ( searchResult.Properties.Contains( "department" ) ) userInfo.Department = (string)searchResult.Properties[ "department" ][ 0 ]; if ( searchResult.Properties.Contains( "mail" ) ) userInfo.EmailAddress = (string)searchResult.Properties[ "mail" ][ 0 ]; if ( searchResult.Properties.Contains( "employeeid" ) ) userInfo.EmployeeId = (string)searchResult.Properties[ "employeeid" ][ 0 ]; if ( searchResult.Properties.Contains( "telephonenumber" ) ) userInfo.BusinessPhone = (string)searchResult.Properties[ "telephonenumber" ][ 0 ]; if ( searchResult.Properties.Contains( "physicalDeliveryOfficeName" ) ) userInfo.PhysicalDeliveryOfficeName = (string)searchResult.Properties[ "physicalDeliveryOfficeName" ][ 0 ]; if ( searchResult.Properties.Contains( "msrtcsip-primaryuseraddress" ) ) userInfo.SipAccount = (string)searchResult.Properties[ "msrtcsip-primaryuseraddress" ][ 0 ]; if ( searchResult.Properties.Contains( "msrtcsip-line" ) ) userInfo.Telephone = (string)searchResult.Properties[ "msrtcsip-line" ][ 0 ]; if ( searchResult.Properties.Contains( "msrtcsip-primaryhomeserver" ) ) userInfo.PrimaryHomeServerDn = ( (string)searchResult.Properties[ "msrtcsip-primaryhomeserver" ][ 0 ] ).Replace( "CN=Lc Services,CN=Microsoft," , "" ); if ( searchResult.Properties.Contains( "userprincipalname" ) ) userInfo.Upn = (string)searchResult.Properties[ "userprincipalname" ][ 0 ]; //Get the IP Dialing Code and extensionfor projects if ( searchResult.Properties.Contains( "extensionAttribute1" ) && searchResult.Properties.Contains( "extensionAttribute2" ) ) { userInfo.OtherTelphone = (string)searchResult.Properties[ "extensionAttribute2" ][ 0 ] + searchResult.Properties[ "extensionAttribute1" ][ 0 ]; } } } catch ( Exception ex ) { userInfo.Message = ex.Message; return userInfo; } } return userInfo; }
/// <summary> /// Get user attrubutes from email address /// </summary> /// <param name="mailAddress">User Email Address</param> /// <returns>UserInfo Object</returns> private UserInfoResponse GetUserAttributes( string mailAddress ) { var userInfo = new UserInfoResponse(); var searchFilter = string.Format( AdSearchFilter , mailAddress ); searcher.Filter = searchFilter; try { var searchResult = searcher.FindOne(); if ( searchResult != null ) { if ( searchResult.Properties.Contains( "title" ) ) userInfo.Title = (string)searchResult.Properties[ "title" ][ 0 ]; if ( searchResult.Properties.Contains( "givenName" ) ) userInfo.FirstName = (string)searchResult.Properties[ "givenName" ][ 0 ]; if ( searchResult.Properties.Contains( "sn" ) ) userInfo.LastName = (string)searchResult.Properties[ "sn" ][ 0 ]; if ( searchResult.Properties.Contains( "cn" ) ) userInfo.DisplayName = (string)searchResult.Properties[ "cn" ][ 0 ]; if ( searchResult.Properties.Contains( "samaccountname" ) ) userInfo.SamAccountName = (string)searchResult.Properties[ "samaccountname" ][ 0 ]; if ( searchResult.Properties.Contains( "department" ) ) userInfo.Department = (string)searchResult.Properties[ "department" ][ 0 ]; if ( searchResult.Properties.Contains( "mail" ) ) userInfo.EmailAddress = (string)searchResult.Properties[ "mail" ][ 0 ]; if ( searchResult.Properties.Contains( "employeeid" ) ) userInfo.EmployeeId = (string)searchResult.Properties[ "employeeid" ][ 0 ]; if ( searchResult.Properties.Contains( "telephonenumber" ) ) userInfo.BusinessPhone = (string)searchResult.Properties[ "telephonenumber" ][ 0 ]; if ( searchResult.Properties.Contains( "physicalDeliveryOfficeName" ) ) userInfo.PhysicalDeliveryOfficeName = (string)searchResult.Properties[ "physicalDeliveryOfficeName" ][ 0 ]; if ( searchResult.Properties.Contains( "msrtcsip-primaryuseraddress" ) ) userInfo.SipAccount = (string)searchResult.Properties[ "msrtcsip-primaryuseraddress" ][ 0 ]; if ( searchResult.Properties.Contains( "msrtcsip-line" ) ) userInfo.Telephone = (string)searchResult.Properties[ "msrtcsip-line" ][ 0 ]; if ( searchResult.Properties.Contains( "msrtcsip-primaryhomeserver" ) ) userInfo.PrimaryHomeServerDn = ( (string)searchResult.Properties[ "msrtcsip-primaryhomeserver" ][ 0 ] ).Replace( "CN=Lc Services,CN=Microsoft," , "" ); if ( searchResult.Properties.Contains( "userprincipalname" ) ) userInfo.Upn = (string)searchResult.Properties[ "userprincipalname" ][ 0 ]; //Get the IP Dialing Code and extensionfor projects if ( searchResult.Properties.Contains( "extensionAttribute1" ) && searchResult.Properties.Contains( "extensionAttribute2" ) ) { userInfo.OtherTelphone = (string)searchResult.Properties[ "extensionAttribute2" ][ 0 ] + searchResult.Properties[ "extensionAttribute1" ][ 0 ]; } return userInfo; } return null; } catch ( Exception ex ) { var argEx = new ArgumentException( "Exception" , "ex" , ex ); throw argEx; } }