Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
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:

C#
[HttpPost]
public ActionResult SaveEmployeeAction(EmployeeViewModel employeeViewModel)
{
    if (ModelState.IsValid)
    {
        var workingThread = new Thread(() => SaveEmployee(employeeViewModel));
        workingThread.Start();

        return RedirectToAction("EmployeeList");
    }
    else
    {
        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 })
            .ToArray();

        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");
                    model.ErrorInfoList.Add(errorInfo);
                }
                else
                {
                    var errorInfo = new HandleErrorInfo(subError.Exception, "EmployeeController", "SaveEmployeeAction");
                    model.ErrorInfoList.Add(errorInfo);
                }
            }
        }
        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?


C#
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)
        {
            tryCount++;
            Thread.Sleep(10000);
            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();
    emailClient.SendEmail();
}
Posted

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900