Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET MVC
I have a MVC3 Application using Entity Framework 4 Code First as the model.
 
In the Employee Controller I have the following Action method:
 
[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?
 

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 15-Feb-13 3:58am

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

  Print Answers RSS
0 OriginalGriff 406
1 /\jmot 180
2 Suraj Sahoo | Coding Passion 170
3 Afzaal Ahmad Zeeshan 159
4 BillWoodruff 154
0 OriginalGriff 8,344
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,634
3 Maciej Los 5,024
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 15 Feb 2013
Copyright © CodeProject, 1999-2014
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