Click here to Skip to main content
13,096,858 members (85,528 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I have a MVC3 Application using Entity Framework 4 Code First as the model.

In the Employee Controller I have the following Action method:

public ActionResult SaveEmployeeAction(EmployeeViewModel employeeViewModel)
    if (ModelState.IsValid)
        var workingThread = new Thread(() => SaveEmployee(employeeViewModel));
        return RedirectToAction("EmployeeList");
        System.Diagnostics.Debug.WriteLine("invalid model state");
        var model = new ErrorViewModel();
        var errors = ModelState
            .Where(x => x.Value.Errors.Count > 0)
            .Select(x => new { x.Key, x.Value.Errors })
        foreach (var error in errors)
            foreach (var subError in error.Errors)
                if (subError.Exception == null)
                    var errorInfo = new HandleErrorInfo(new Exception(subError.ErrorMessage), "EmployeeController", "SaveEmployeeAction");
                    var errorInfo = new HandleErrorInfo(subError.Exception, "EmployeeController", "SaveEmployeeAction");
        return View("Error", model);

I am using a separate thread because when we create a new employee, it needs to be created with a third party application. This is done by inserting a record in a table, which is then polled by a service which raises a flag to tell the third party application to create the employee. Once this is done, I get the newly created employee data and update it.

All works well when I launch the application from VS2010 with the Web properties set to "Use Visual Studio Development Server" and NTLM Authentication checked.

However, when I run the application in the test environment, I set the Web Properties to "Use Local IIS Web Server". At this point, it all works until I launch the thread. Then Entity throws a System.Data.EntityException with the Inner Exception being a System.Data.SqlClient.SqlException {"Login failed for user 'NMC\MSPD-9NBQHS1$'."}, where MSPD-9NBQHS1 is the name of the Test Server. All other calls made to Entity work as normal, except when made through the thread. It appears that the logged in user's credentials are being passed through to SQL Server via Entity properly except when the thread is launched, then the IIS account credentials appear to be passed.

In IIS Authentication, I have ASP.Net Impersonation and Window Authentication enabled and all others disabled. It is using the ASP.Net v4.o Application Pool.

I'm probably missing some simple configuration setting here, anyone have some ideas?

private void SaveEmployee(EmployeeViewModel employeeViewModel)
    if (!String.IsNullOrEmpty(employeeViewModel.NewEmployeeIdentifier))
        // Insert a row into NMC_ExternalFlags
        // This should create a new employee_information record where memTemp = model.NewEmployeeIdentifier
        _employeeService.CreateExternalFlag("EmployeeInformation_New", "", employeeViewModel.NewEmployeeIdentifier);
        // Find the employee by the NewEmployeeIdentifier
        var newEmployee = _employeeService.GetEmployeeByEmployeeIdentifier(employeeViewModel.NewEmployeeIdentifier);
        Int32 tryCount = 0;
        while (newEmployee == null)
            if (tryCount > 60)
                throw new Exception("Unable to get new employee from Metastorm");
            newEmployee = _employeeService.GetEmployeeByEmployeeIdentifier(employeeViewModel.NewEmployeeIdentifier);
        // Set the employeeViewModel.EFolderId
        employeeViewModel.EFolderId = newEmployee.EFolderid;
        employeeViewModel.SelectedEmployee.EmployeeID = newEmployee.EmployeeID;
        employeeViewModel.SelectedEmployee.NewEmployeeIdentifier = newEmployee.NewEmployeeIdentifier;
    employeeViewModel.SelectedEmployee.EFolderid = employeeViewModel.EFolderId;
    _employeeService.UpdateEmployee(employeeViewModel.SelectedEmployee, employeeViewModel.EFolderId);
    // Add email here
    EmailClient emailClient = new EmailClient();
Posted 15-Feb-13 2:58am

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web03 | 2.8.170813.1 | Last Updated 15 Feb 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100