如何在当前场景中设置主体

本文关键字:设置 主体 | 更新日期: 2023-09-27 18:12:50

考虑下面的代码,目标是设置System.Threading.Thread.CurrentPrincipal并在另一个窗口....中检索它简单的代码与注释,以澄清我想要实现的。

        Task myTask = Task.Factory.StartNew(new Action(() =>
        {
            // do in another task so that the UI does not freeze
            var user = modelLogin.Login(username, password);
            // run on the current dispatcher thread to show results of login
            currentDispatcher.BeginInvoke(new Action(() =>
            {
                if (user == null)
                {
                    errorText = "Authentication failed";
                }
                else
                {
                    // set the principal
                    // how ever setting the value here, does not really set it on the main thread
                    // what i mean by this is that when the login window close i want to get 
                    // the  principal i set here in another window
                    // but it does not seem to work that way
                    // how to propely set this value in this current situation?
                    System.Threading.Thread.CurrentPrincipal = authentication;
                }
                ToggleIsBusy();
                CommandManager.InvalidateRequerySuggested();
            }));
        }));

有什么好主意吗?

如何在当前场景中设置主体

不设置CurrentPrincipal of Thread, 通过在AppDomain上设置它来为所有线程设置CurrentPrincipal, AppDomain将为包括主线程在内的所有线程设置CurrentPrincipal :

AppDomain.CurrentDomain.SetThreadPrincipal(authentication);