Tuesday 27 May 2014

Unable to locate Database backup (.bak) file during Database restore.


Recently I was given a Database backup (.bak) file to restore database. In SQL Management Studio when I tried to locate the backup file, it was not visible to me.  



Problem was the permissions issue. SQL Server Service was running using 'Network Service' account but 'Network Service' account did not have permissions to access the backup folder. 

To check the current account of SQL Server Service. Go to Control Panel > Administrative Tools > Services and the double click on SQL Server Service. It will open properties, then click on 'Log On' tab to see the account used to Log on by SQL Server Service. Following picture shows the account used for service.



Now right click on Backup folder to open properties. Click on Security tab and then click on Edit.. 
it will open a new window  called 'Permissions for <folder-name>'. Click on Add and add Network Service and give full control and press OK to close both windows. 

  
Now restart the SQL Server Management and try to restore the backup again. Backup (.bak) file should be visible now. 

P.S. Hayer

Please check my other blog here: Hayer's MS Dynamics CRM Blog

Friday 7 March 2014

How to Access User Details from Active Directory using C#

You might come across this situation of you are developing a Web Application with Windows Authentication and trying to get the User Details. It's pretty simple if your all target users are from same domain. Following code fetch the details for you.
    // Get user name
    string userName = User.Identity.Name;

    // Get domain
    using (PrincipalContext principalContext = new PrincipalContext(ContextType.Domain))
   {
        // Get User Details
        using (UserPrincipal user = UserPrincipal.FindByIdentity(principalContext, userName))
        {
            if (user != null)
            {
                var firstName = user.GivenName;
                var emailAddress = user.EmailAddress;         
            }
        }
    }

But if you have Trusted Relationship between 2-3 different domains and your user can be anyone from any of those domains. To get the details of user, who is not on the domain where application is deployed. Following code should work for you.
   // Get user name
    string userName = User.Identity.Name;
    var domainName = username.Split('\\')[0];

    // Getting domain
    var directoryContext = new DirectoryContext(DirectoryContextType.Domain, domainName);
    Domain domain = Domain.GetDomain(directoryContext);
    using (PrincipalContext principalContext = new PrincipalContext(ContextType.Domain, domain.Name))
   {
        // Get User Details
        using (UserPrincipal user = UserPrincipal.FindByIdentity(principalContext, userName))
        {
            if (user != null)
            {
                var firstName = user.GivenName;           
                var emailAddress = user.EmailAddress;
            }
        }
    }

PS Hayer
Please check my other (non-CRM) blog here: Hayer's MS Dynamics CRM Blog