MVC3身份验证和线程 [英] MVC3 authentication and Threading

查看:77
本文介绍了MVC3身份验证和线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用Entity Framework 4 Code First作为模型的MVC3应用程序。



在员工控制器中我有以下Action方法:



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);
    }
}









我我正在使用一个单独的线程,因为当我们创建一个新员工时,需要使用第三方应用程序创建它。这是通过在表中插入记录来完成的,然后由一个服务进行轮询,该服务会引发一个标志来告诉第三方应用程序创建该员工。完成后,我会获得新创建的员工数据并进行更新。



当我从VS2010启动应用程序并将Web属性设置为使用时,一切正常Visual Studio开发服务器和NTLM身份验证已选中。



但是,当我在测试环境中运行应用程序时,我将Web属性设置为使用本地IIS Web服务器。此时,一切正常,直到我启动线程。然后Entity抛出一个System.Data.EntityException,其内部异常是System.Data.SqlClient.SqlException {用户登录失败''NMC \ MSPD-9NBQHS1 $''。},其中MSPD-9NBQHS1是名称测试服务器。除了通过线程进行外,对Entity的所有其他调用都正常工作。看来,登录用户的凭据正在通过实体正确传递到SQL Server,除非线程启动时,IIS帐户凭据似乎已通过。



在IIS身份验证中,我启用了ASP.Net模拟和窗口身份验证,并禁用了所有其他身份验证。它正在使用ASP.Net v4.o应用程序池。



我可能在这里错过了一些简单的配置设置,任何人都有一些想法吗?









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();
}

推荐答案

''。},其中MSPD-9NBQHS1是测试服务器的名称。所有其他调用除非通过线程进行,否则对实体的工作正常。看起来登录用户的凭据正在通过实体正确传递到SQL Server,除非线程启动,然后IIS帐户凭据似乎是通过。



在IIS身份验证中,我启用了ASP.Net模拟和窗口身份验证,所有其他人都被禁用。它使用的是ASP.Net v4.o应用程序池。



我可能在这里错过了一些简单的配置设置,任何人都有一些想法吗?





''."}, 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();
}


这篇关于MVC3身份验证和线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆