Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / November 2006

Tip: Looking for answers? Try searching our database.

when CurrentPrincipal is set in async call back, NET 2.0 loses CurrentPrincipal

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
kochobay@gmail.com - 30 Oct 2006 23:04 GMT
i use a form to login the system.
After database checks System.Threading.Thread.CurrentPrincipal is being
set in async call back.
During database chekcs, user interface displays a progress bar. And
this works fine with .NET 1.1.
But i newly ported application to NET 2.0 (Visual Studio 2005) and
after async call back, System.Threading.Thread.CurrentPrincipal is
resetting to empty GenericPrincipal.

I think it's a bug.

Any suggestion?
Andy - 01 Nov 2006 18:15 GMT
Not a bug; the Async is causing code to run on another thread.  That's
the thread getting the principal object set.  The thread which kicks
off the async operation never had its principal changed.

Andy

> i use a form to login the system.
> After database checks System.Threading.Thread.CurrentPrincipal is being
[quoted text clipped - 8 lines]
>
> Any suggestion?
kochobay@gmail.com - 02 Nov 2006 00:11 GMT
Thanks for reply Andy,

But there exists something strange.
I have a custom Principal class implement IPrincipal interface.
i have two delegate ike below

protected delegate object AsynchronousCall(params object[] args);
protected delegate void AsynchronousCallEnd(object arg);

and my background thread method is

IPrincipal Logon(string username, string password)
{
   Principal myPrincipal = new Principal();
   //Some login operations and populate myPrincipal.
   return myPrincipal;
}

and i call this Logon method on button click
private void okButton_Click(object sender, System.EventArgs e)
{
 AsynchronousCall asyncCall = new AsynchronousCall(Logon);
 asyncCall.BeginInvoke(new object[] {uname.pwd}, new
AsyncCallback(OnLogonEnd), asyncCall);
}

and when my background thread finished its work , OnLogonEnd method
called back.

protected void OnLogonEnd(IAsyncResult ar)
{
 AsynchronousCall d = (AsynchronousCall)ar.AsyncState;
 object returnValue = d.EndInvoke(ar);
 this.OnAsynchronousCallEnd(new object[] {returnValue});
 this.Invoke(new AsynchronousCallEnd(this.OnAsynchronousCallEnd),
returnValue);
}

protected override void OnAsynchronousCallEnd(object arg)
{
 System.Threading.Thread.CurrentPrincipal = (IPrincipal) arg;
 new XForm().Show();
}

i debugged many times and when code entered XForm OnLoad method
System.Threading.Thread.CurrentPrincipal has correct value.

private void XForm_Load(object sender, System.EventArgs e)
{
 System.Security.Principal.IPrincipal p =
System.Threading.Thread.CurrentPrincipal;
 MessageBox.Show(p.Identity.Name);
} // has correct value

But i click on the XForm, i saw the incorrect GenericPrincipal in the
CurrentPrincipal.

private void XForm_Click(object sender, System.EventArgs e)
{
 System.Security.Principal.IPrincipal p =
System.Threading.Thread.CurrentPrincipal;
 MessageBox.Show(p.Identity.Name);
} // has INCORRECT value

Thanks a lof for your interest, again.
kochobay@gmail.com - 02 Nov 2006 00:17 GMT
when i write method OnLogonEnd here , i made mistake sorry.

it is like this;

protected void OnLogonEnd(IAsyncResult ar)
{
 AsynchronousCall d = (AsynchronousCall)ar.AsyncState;
 object returnValue = d.EndInvoke(ar);
 this.Invoke(new AsynchronousCallEnd(this.OnAsynchronousCallEnd),
returnValue);
}
kochobay@gmail.com - 02 Nov 2006 22:18 GMT
I found a solution , I think...

When ok button clicked ,
i called login and synchronization process (that takes long time) and i
did not want to freeze my main form, and wanted to display some
information about the process.
For this reason i called login and synchronization process on another
THREAD. In this background thread, i pumped several messages back to
the main form. For this pupose i used Form.Invoke() method for thread
safety. And when the background thread finished its work, i set a
Principal to System.Threading.Thread.CurrentPrincipal.

I use an architecture same as indicated at link below .
http://www.devx.com/codemag/Article/20639/0/page/3

And It was good in .NET 1.1 framework.

When i ported to NET 2.0, after background process completed , i
haven't seen the principal that i put into
System.Threading.Thread.CurrentPrincipal. It was lost.

Long time i drove crazy...

At last i found a workaround.
First, in callback method i set my principal as default principal
 protected void OnLogonEnd(IAsyncResult ar)
 {
   ....
   AppDomain.CurrentDomain.SetThreadPrincipal(myPrincipal);
 }

Second, after invocation of the background thread as mentioned previous
post

 AsynchronousCall asyncCall = new AsynchronousCall(Logon);
 asyncCall.BeginInvoke(new object[] {uname.pwd}, new
AsyncCallback(OnLogonEnd), asyncCall);

i added the followwing code

 if
(!System.Threading.Thread.CurrentPrincipal.Identity.IsAuthenticated)
          System.Threading.Thread.CurrentPrincipal = null;

This is bug or not. I do not know.
Only thing that i know is that there exists a tedious difference on
threading and operating on CurrentPrincipal between 1.1 and 2.0...

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.