使用 ASP.NET 实现模拟
运行第三方软件(例如 SQL Server®)时,有时需要使用模拟在 ASP.NET 应用程序中执行 Windows® 身份验证。
在已部署的应用程序中,模拟凭据从 IIS 传入。但是,由于模拟是基于每个线程进行操作的,因此在多线程部署的应用程序中处理 MATLAB® Runtime 线程时有时会出现问题。
使用以下示例在 MATLAB 文件中打开和关闭模拟,以避免由 MATLAB Runtime 线程处理问题引起的问题。
#include mex.h
#include windows.h
/*
*This mex function is called with a single int which
*represents the user
*identity token. We use this token to impersonate a
*user on the interpreter
*thread. This acts as a workaround for ASP.NET
*applications that use
*impersonation to pass the proper credentials
*to SQL Server for windows
*authentication. The function returns non zero status
*for success, zero otherwise.
**/
void mexFunction( int nlhs,
mxArray * plhs[],
int nrhs,
const mxArray * prhs[] )
{
plhs[0] = mxCreateDoubleScalar(0); //return status
HANDLE hToken =
reinterpret_cast(*(mwSize *)mxGetData(prhs[0]));
if(nrhs != 1)
{
mexErrMsgTxt("Incorrect number of input argument(s).
Expecting 1.");
}
int hr;
if(!(hr = ImpersonateLoggedOnUser(hToken)))
{
mexErrMsgTxt("Error impersonating.\n");
}
*(mxGetPr(plhs[0])) = hr;
}
#include mex.h
#include windows.h
/*
*This mex function reverts to the old identity on the
interpreter thread **/
void mexFunction( int nlhs,
mxArray * plhs[],
int nrhs,
const mxArray * prhs[] )
{
if(!RevertToSelf())
{
mexErrMsgTxt("Failed to revert to the old
identity.");
}
}
Monitor.Enter(someObj);
DeployedComponent.DeployedComponentClass myComp;
try
{
System.Security.Principal.WindowsIdentity myIdentity =
System.Security.Principal.WindowsIdentity.GetCurrent();
//short circuit if user app is not impersonated
if(myIdentity.isImpersonated())
{
myComp = new DeployedComponent.
DeployedComponentClass ();
//Run Users code
MWArray[] output = myComp.impersonateUser(1,
getToken());
}
else
{
//Run Users code
}
}
Catch(Exception e)
{
}
finally
{
if(myComp!=null)
myComp.stopImpersonation();
Monitor.Exit(someObj;)
}
//
//
//Utility method to read the token for the current user
//and wraps it in a MWArray private MWNumericArray getToken()
{
System.Security.Principal.WindowsIdentity myIdentity =
System.Security.Principal.WindowsIdentity.GetCurrent();
MWNumericArray a = null;
if (IntPtr.Size == 4)
{
int intToken = myIdentity.Token.ToInt32();
a = new MWNumericArray(intToken, false);
}
else
{
Int64 intToken = myIdentity.Token.ToInt64();
a = new MWNumericArray(intToken, false);
}
return a;
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
