需要将Windows服务与非管理员用户进程同步

发布于 2024-12-11 04:29:29 字数 312 浏览 0 评论 0原文

我正在使用 (.NET2.0) 服务定期执行特权操作,并通过 IPC 通过非管理员用户 (.NET2.0) 进程将结果返回到共享内存。

我在从管理员帐户进行开发时一直使用全局命名的互斥体,但是当我在有限帐户上尝试该应用程序时,出现错误:

其他信息:对路径“Global\timersyncu33sc3c2sd42frandomlynamedmutexoijfvgf9v3f32”的访问被拒绝。

非特权用户是否可以通过其他方式与服务交互?或者我应该只共享轮询和更新时间的周期并希望这些值能够自动写入/读取?

I am using a (.NET2.0) service to periodically carry out privileged actions and return the result to shared memory, via IPC, with a non-admin user's (.NET2.0) process.

I have been using globally named mutexes while developing from my administrator account but when I come to try the application on a limited account I get the error:

Additional information: Access to the path 'Global\timersyncu33sc3c2sd42frandomlynamedmutexoijfvgf9v3f32' is denied.

Is there some other way a non-privileged user can interact with a Service? Or should I just share the period of polling and update time and hope those values get written/read atomically?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浴红衣 2024-12-18 04:29:29

我会使用 WCF 与服务进行对话。这消除了特权/身份问题。
但是,由于您的代码位于 .NET 2.0 中,因此您可以使用 Remoting 或 NamedPipes 与服务通信。

I would talk to the service using WCF. This eliminates the privileges/identity problem.
However since your code is in .NET 2.0 you could use Remoting or NamedPipes to talk to the service.

泡沫很甜 2024-12-18 04:29:29

我发现了以下方法,可以在有限用户登录并启动 GUI 时向他们授予访问权限。请注意,函数 getUsername(/*somehow*/); 有多个版本,我没有列出在 XP 上运行的实现,我确信还有其他方法,尽管我发现了 4 种方法中的 3 种不适合我。

void grantMutexToCurUser(Mutex ^%fpMutex) {
    try {
        fpMutex = Mutex::OpenExisting( ServerGUIBridge::NAMEDMUTEXFORTIMERSYNC,
            static_cast<MutexRights>(
            MutexRights::ReadPermissions | MutexRights::ChangePermissions) );

        MutexSecurity^ mSec = fpMutex->GetAccessControl();
        String^ user;

        try {
            user = getUsername(/*somehow*/);
            Trace::WriteLine( DateTime::Now.ToLongTimeString() + " - Granting mutex access to: " 
                + user , "grantMutexToCurUser" );
        } catch (Exception ^ex) {
            Trace::WriteLine( "getUsername: " + ex->Message, "grantMutexToCurUser" );
        }

        // First, the rule that denied the current user the right to enter and
        // release the mutex must be removed.
        MutexAccessRule^ rule = gcnew MutexAccessRule( user,
            static_cast<MutexRights>(MutexRights::Synchronize
            | MutexRights::Modify), AccessControlType::Deny );
        mSec->RemoveAccessRule( rule );

        // Now grant the user the correct rights.
        rule = gcnew MutexAccessRule( user,
            static_cast<MutexRights>(MutexRights::Synchronize
            | MutexRights::Modify), AccessControlType::Allow );
        mSec->AddAccessRule( rule );

        fpMutex->SetAccessControl( mSec );

        // Open the mutex with (MutexRights.Synchronize | MutexRights.Modify), the
        // rights required to enter and release the mutex.
        fpMutex = Mutex::OpenExisting( ewfmon::ServerGUIBridge::NAMEDMUTEXFORTIMERSYNC );
        //noThrowRelease(fpMutex);
    }
    catch ( UnauthorizedAccessException^ ex ) 
    {
        Trace::WriteLine( DateTime::Now.ToLongTimeString() + " - Unable to change permissions: "
            + ex->Message, "grantMutexToCurUser" );
    }
}

I found the following method of granting access to my limited users as they log on and fire up their GUI. Note the function getUsername(/*somehow*/); had several incarnations and I'm not listing the implementation that worked on XP, I'm sure there are other ways though 3 of the 4 I found didn't work for me.

void grantMutexToCurUser(Mutex ^%fpMutex) {
    try {
        fpMutex = Mutex::OpenExisting( ServerGUIBridge::NAMEDMUTEXFORTIMERSYNC,
            static_cast<MutexRights>(
            MutexRights::ReadPermissions | MutexRights::ChangePermissions) );

        MutexSecurity^ mSec = fpMutex->GetAccessControl();
        String^ user;

        try {
            user = getUsername(/*somehow*/);
            Trace::WriteLine( DateTime::Now.ToLongTimeString() + " - Granting mutex access to: " 
                + user , "grantMutexToCurUser" );
        } catch (Exception ^ex) {
            Trace::WriteLine( "getUsername: " + ex->Message, "grantMutexToCurUser" );
        }

        // First, the rule that denied the current user the right to enter and
        // release the mutex must be removed.
        MutexAccessRule^ rule = gcnew MutexAccessRule( user,
            static_cast<MutexRights>(MutexRights::Synchronize
            | MutexRights::Modify), AccessControlType::Deny );
        mSec->RemoveAccessRule( rule );

        // Now grant the user the correct rights.
        rule = gcnew MutexAccessRule( user,
            static_cast<MutexRights>(MutexRights::Synchronize
            | MutexRights::Modify), AccessControlType::Allow );
        mSec->AddAccessRule( rule );

        fpMutex->SetAccessControl( mSec );

        // Open the mutex with (MutexRights.Synchronize | MutexRights.Modify), the
        // rights required to enter and release the mutex.
        fpMutex = Mutex::OpenExisting( ewfmon::ServerGUIBridge::NAMEDMUTEXFORTIMERSYNC );
        //noThrowRelease(fpMutex);
    }
    catch ( UnauthorizedAccessException^ ex ) 
    {
        Trace::WriteLine( DateTime::Now.ToLongTimeString() + " - Unable to change permissions: "
            + ex->Message, "grantMutexToCurUser" );
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文