473,394 Members | 1,812 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Public information

Hallo,

In my application, the user must logon with a username and a password. In
all the tables in my database, there is a field user. By saving new or
changed records, the username is writed to that field.

Now I have a class Session with the User-information.
By opening the program, I have code like this on the mainform:

Session sess = new Session().
sess.UserName = "John";

When I open a new form, then I do this:

frmNew frm = new frmNew(sess);

When I write to the Database, I do this:

Article art = new Article(sess);

etc.

This is working, but I was asking by myself, that maybe there must be
another way to do this.
That I can store the information in a public class or struct and that I can
read this from every other form or class in my program.

Is there another way? And how can I do that?
thank you
Gerrit
Dec 9 '06 #1
17 1431
Hi Gerrit,
In my application, the user must logon with a username and a password. In
all the tables in my database, there is a field user. By saving new or
changed records, the username is writed to that field.
You should think about normalizing your database by creating a Users table
and assigning a unique identifier (GUID or INT) to each user. Then, you can
add a foreign key constraint to each table where you want to keep track of
the user that performed the last modification. Instead of storing a name
you'd store the user's identifier in all of the related tables.

"Database Normalization"
http://en.wikipedia.org/wiki/Database_normalization
Now I have a class Session with the User-information.
By opening the program, I have code like this on the mainform:

Session sess = new Session().
sess.UserName = "John";

When I open a new form, then I do this:

frmNew frm = new frmNew(sess);

When I write to the Database, I do this:

Article art = new Article(sess);

etc.

This is working, but I was asking by myself, that maybe there must be
another way to do this.
That I can store the information in a public class or struct and that I
can read this from every other form or class in my program.

Is there another way? And how can I do that?
Yes, you can add a static member to your business object:

class User
{
private User currentUser;
public static User CurrentUser { get { return currentUser; } }

...
}

After you identify the user set the property, for example:

User user = new User("Gerrit");
User.CurrentUser = user;

And when you need to access it in another business object just read the
property:

class Article
{
public void Save()
{
User currentUser = User.CurrentUser;

...
}
}

--
Dave Sexton
Dec 9 '06 #2
The solution depends. One way you could solve this is by creating a
GenericIdentity and setting the current principal:
System.Security.Principal.IIdentity identity = new
System.Security.Principal.GenericIdentity("USERNAM E");
System.Threading.Thread.CurrentPrincipal = new
System.Security.Principal.GenericPrincipal(identit y, new string[0]);

Then anywhere in your code you can get the "USERNAME" as follows:
System.Threading.Thread.CurrentPrincipal.Identity. Name

If you are using SQL Server authentication you could let the database set
the user field with every insert, update ... using triggers.

Gabriel Lozano-Morán

"Gerrit" <gs*************@hotmail.comwrote in message
news:c1***************************@news.chello.nl. ..
Hallo,

In my application, the user must logon with a username and a password. In
all the tables in my database, there is a field user. By saving new or
changed records, the username is writed to that field.

Now I have a class Session with the User-information.
By opening the program, I have code like this on the mainform:

Session sess = new Session().
sess.UserName = "John";

When I open a new form, then I do this:

frmNew frm = new frmNew(sess);

When I write to the Database, I do this:

Article art = new Article(sess);

etc.

This is working, but I was asking by myself, that maybe there must be
another way to do this.
That I can store the information in a public class or struct and that I
can read this from every other form or class in my program.

Is there another way? And how can I do that?
thank you
Gerrit

Dec 9 '06 #3
Hi Gerrit,

Correction in my code sample:
public static User CurrentUser { get { return currentUser; } }
public static User CurrentUser {
get { return currentUser; }
set { currentUser = value; } // required for write-access
}

--
Dave Sexton

"Dave Sexton" <dave@jwa[remove.this]online.comwrote in message
news:OA*************@TK2MSFTNGP06.phx.gbl...
Hi Gerrit,
>In my application, the user must logon with a username and a password. In
all the tables in my database, there is a field user. By saving new or
changed records, the username is writed to that field.

You should think about normalizing your database by creating a Users table
and assigning a unique identifier (GUID or INT) to each user. Then, you
can add a foreign key constraint to each table where you want to keep
track of the user that performed the last modification. Instead of
storing a name you'd store the user's identifier in all of the related
tables.

"Database Normalization"
http://en.wikipedia.org/wiki/Database_normalization
>Now I have a class Session with the User-information.
By opening the program, I have code like this on the mainform:

Session sess = new Session().
sess.UserName = "John";

When I open a new form, then I do this:

frmNew frm = new frmNew(sess);

When I write to the Database, I do this:

Article art = new Article(sess);

etc.

This is working, but I was asking by myself, that maybe there must be
another way to do this.
That I can store the information in a public class or struct and that I
can read this from every other form or class in my program.

Is there another way? And how can I do that?

Yes, you can add a static member to your business object:

class User
{
private User currentUser;
public static User CurrentUser { get { return currentUser; } }

...
}

After you identify the user set the property, for example:

User user = new User("Gerrit");
User.CurrentUser = user;

And when you need to access it in another business object just read the
property:

class Article
{
public void Save()
{
User currentUser = User.CurrentUser;

...
}
}

--
Dave Sexton


Dec 9 '06 #4
Gerrit,

I go for the answer from Gabriel, but just a little addition to your own
code. I would not use the name Session in this way. It is an attribute in
ASPNET what is almost a keyword.

Cor

"Gerrit" <gs*************@hotmail.comschreef in bericht
news:c1***************************@news.chello.nl. ..
Hallo,

In my application, the user must logon with a username and a password. In
all the tables in my database, there is a field user. By saving new or
changed records, the username is writed to that field.

Now I have a class Session with the User-information.
By opening the program, I have code like this on the mainform:

Session sess = new Session().
sess.UserName = "John";

When I open a new form, then I do this:

frmNew frm = new frmNew(sess);

When I write to the Database, I do this:

Article art = new Article(sess);

etc.

This is working, but I was asking by myself, that maybe there must be
another way to do this.
That I can store the information in a public class or struct and that I
can read this from every other form or class in my program.

Is there another way? And how can I do that?
thank you
Gerrit

Dec 9 '06 #5
Hi Cor,

I think a more OO approach is better. If the OP wants, there is no reason
why the User object can't just implement IIdentity as well, although I think

User.CurrentUser.Name

is much more legible than

System.Threading.Thread.CurrentPrincipal.Identity. Name

so I don't know why anyone would prefer the latter :)

--
Dave Sexton

"Cor Ligthert [MVP]" <no************@planet.nlwrote in message
news:uM**************@TK2MSFTNGP04.phx.gbl...
Gerrit,

I go for the answer from Gabriel, but just a little addition to your own
code. I would not use the name Session in this way. It is an attribute in
ASPNET what is almost a keyword.

Cor

"Gerrit" <gs*************@hotmail.comschreef in bericht
news:c1***************************@news.chello.nl. ..
>Hallo,

In my application, the user must logon with a username and a password. In
all the tables in my database, there is a field user. By saving new or
changed records, the username is writed to that field.

Now I have a class Session with the User-information.
By opening the program, I have code like this on the mainform:

Session sess = new Session().
sess.UserName = "John";

When I open a new form, then I do this:

frmNew frm = new frmNew(sess);

When I write to the Database, I do this:

Article art = new Article(sess);

etc.

This is working, but I was asking by myself, that maybe there must be
another way to do this.
That I can store the information in a public class or struct and that I
can read this from every other form or class in my program.

Is there another way? And how can I do that?
thank you
Gerrit


Dec 9 '06 #6
Thank you all for the answers.

For now, the answer from Dave was where I was looking for, but I save the
others for later.

And I do not use the name Session in my application, but a dutch word
Sessie. I only translate it for my question here. thank you.

Gerrit.

"Dave Sexton" <dave@jwa[remove.this]online.comschreef in bericht
news:%2******************@TK2MSFTNGP03.phx.gbl...
Hi Cor,

I think a more OO approach is better. If the OP wants, there is no reason
why the User object can't just implement IIdentity as well, although I
think

User.CurrentUser.Name

is much more legible than

System.Threading.Thread.CurrentPrincipal.Identity. Name

so I don't know why anyone would prefer the latter :)

--
Dave Sexton

"Cor Ligthert [MVP]" <no************@planet.nlwrote in message
news:uM**************@TK2MSFTNGP04.phx.gbl...
>Gerrit,

I go for the answer from Gabriel, but just a little addition to your own
code. I would not use the name Session in this way. It is an attribute in
ASPNET what is almost a keyword.

Cor

"Gerrit" <gs*************@hotmail.comschreef in bericht
news:c1***************************@news.chello.nl ...
>>Hallo,

In my application, the user must logon with a username and a password.
In all the tables in my database, there is a field user. By saving new
or changed records, the username is writed to that field.

Now I have a class Session with the User-information.
By opening the program, I have code like this on the mainform:

Session sess = new Session().
sess.UserName = "John";

When I open a new form, then I do this:

frmNew frm = new frmNew(sess);

When I write to the Database, I do this:

Article art = new Article(sess);

etc.

This is working, but I was asking by myself, that maybe there must be
another way to do this.
That I can store the information in a public class or struct and that I
can read this from every other form or class in my program.

Is there another way? And how can I do that?
thank you
Gerrit



Dec 9 '06 #7
Dave,

I agree with you that the CurrentPrincipal use is not the most logical.

I was more pointing on the inbuild security system.

Cor

"Dave Sexton" <dave@jwa[remove.this]online.comschreef in bericht
news:%2******************@TK2MSFTNGP03.phx.gbl...
Hi Cor,

I think a more OO approach is better. If the OP wants, there is no reason
why the User object can't just implement IIdentity as well, although I
think

User.CurrentUser.Name

is much more legible than

System.Threading.Thread.CurrentPrincipal.Identity. Name

so I don't know why anyone would prefer the latter :)

--
Dave Sexton

"Cor Ligthert [MVP]" <no************@planet.nlwrote in message
news:uM**************@TK2MSFTNGP04.phx.gbl...
>Gerrit,

I go for the answer from Gabriel, but just a little addition to your own
code. I would not use the name Session in this way. It is an attribute in
ASPNET what is almost a keyword.

Cor

"Gerrit" <gs*************@hotmail.comschreef in bericht
news:c1***************************@news.chello.nl ...
>>Hallo,

In my application, the user must logon with a username and a password.
In all the tables in my database, there is a field user. By saving new
or changed records, the username is writed to that field.

Now I have a class Session with the User-information.
By opening the program, I have code like this on the mainform:

Session sess = new Session().
sess.UserName = "John";

When I open a new form, then I do this:

frmNew frm = new frmNew(sess);

When I write to the Database, I do this:

Article art = new Article(sess);

etc.

This is working, but I was asking by myself, that maybe there must be
another way to do this.
That I can store the information in a public class or struct and that I
can read this from every other form or class in my program.

Is there another way? And how can I do that?
thank you
Gerrit



Dec 9 '06 #8
Can someone please explain me why this is not the most logical solution? He
clearly says that the user has to logon. Is there any better solution than
to use a generic principal object?

Beside the fact that it violates the Law of Demeter I don't see what is
wrong with calling System.Threading.Thread.CurrentPrincipal.Identity. Name

Gabriel Lozano-Morán
"Cor Ligthert [MVP]" <no************@planet.nlwrote in message
news:ec**************@TK2MSFTNGP02.phx.gbl...
Dave,

I agree with you that the CurrentPrincipal use is not the most logical.

I was more pointing on the inbuild security system.

Cor

"Dave Sexton" <dave@jwa[remove.this]online.comschreef in bericht
news:%2******************@TK2MSFTNGP03.phx.gbl...
>Hi Cor,

I think a more OO approach is better. If the OP wants, there is no
reason why the User object can't just implement IIdentity as well,
although I think

User.CurrentUser.Name

is much more legible than

System.Threading.Thread.CurrentPrincipal.Identity. Name

so I don't know why anyone would prefer the latter :)

--
Dave Sexton

"Cor Ligthert [MVP]" <no************@planet.nlwrote in message
news:uM**************@TK2MSFTNGP04.phx.gbl...
>>Gerrit,

I go for the answer from Gabriel, but just a little addition to your own
code. I would not use the name Session in this way. It is an attribute
in ASPNET what is almost a keyword.

Cor

"Gerrit" <gs*************@hotmail.comschreef in bericht
news:c1***************************@news.chello.n l...
Hallo,

In my application, the user must logon with a username and a password.
In all the tables in my database, there is a field user. By saving new
or changed records, the username is writed to that field.

Now I have a class Session with the User-information.
By opening the program, I have code like this on the mainform:

Session sess = new Session().
sess.UserName = "John";

When I open a new form, then I do this:

frmNew frm = new frmNew(sess);

When I write to the Database, I do this:

Article art = new Article(sess);

etc.

This is working, but I was asking by myself, that maybe there must be
another way to do this.
That I can store the information in a public class or struct and that I
can read this from every other form or class in my program.

Is there another way? And how can I do that?
thank you
Gerrit



Dec 9 '06 #9
Hi Gabriel,

Well it could be useful in certain circumstances, I was just suggesting that
an OO approach is much easier to use and understand.

Actually, I'm using that in a project I'm working on right now (although
I've implemented IIdentity in a strong-Typed DataRow). What I realized
after doing that is it was pointless. I ended up using my static Program
class to aggregate a bunch of static info for the runtime and I realized
that adding a CurrentUser property was much simpler, so I use
Program.CurrentUser in my app now. That's much nicer than
Thread.CurrentPrincipal.Identity.Name, don't you agree?

--
Dave Sexton

"Gabriel Lozano-Morán" <ab***@frontbridge.comwrote in message
news:ul**************@TK2MSFTNGP05.phx.gbl...
Can someone please explain me why this is not the most logical solution?
He clearly says that the user has to logon. Is there any better solution
than to use a generic principal object?

Beside the fact that it violates the Law of Demeter I don't see what is
wrong with calling System.Threading.Thread.CurrentPrincipal.Identity. Name

Gabriel Lozano-Morán
"Cor Ligthert [MVP]" <no************@planet.nlwrote in message
news:ec**************@TK2MSFTNGP02.phx.gbl...
>Dave,

I agree with you that the CurrentPrincipal use is not the most logical.

I was more pointing on the inbuild security system.

Cor

"Dave Sexton" <dave@jwa[remove.this]online.comschreef in bericht
news:%2******************@TK2MSFTNGP03.phx.gbl. ..
>>Hi Cor,

I think a more OO approach is better. If the OP wants, there is no
reason why the User object can't just implement IIdentity as well,
although I think

User.CurrentUser.Name

is much more legible than

System.Threading.Thread.CurrentPrincipal.Identity. Name

so I don't know why anyone would prefer the latter :)

--
Dave Sexton

"Cor Ligthert [MVP]" <no************@planet.nlwrote in message
news:uM**************@TK2MSFTNGP04.phx.gbl...
Gerrit,

I go for the answer from Gabriel, but just a little addition to your
own code. I would not use the name Session in this way. It is an
attribute in ASPNET what is almost a keyword.

Cor

"Gerrit" <gs*************@hotmail.comschreef in bericht
news:c1***************************@news.chello. nl...
Hallo,
>
In my application, the user must logon with a username and a password.
In all the tables in my database, there is a field user. By saving new
or changed records, the username is writed to that field.
>
Now I have a class Session with the User-information.
By opening the program, I have code like this on the mainform:
>
Session sess = new Session().
sess.UserName = "John";
>
When I open a new form, then I do this:
>
frmNew frm = new frmNew(sess);
>
When I write to the Database, I do this:
>
Article art = new Article(sess);
>
etc.
>
This is working, but I was asking by myself, that maybe there must be
another way to do this.
That I can store the information in a public class or struct and that
I can read this from every other form or class in my program.
>
Is there another way? And how can I do that?
thank you
Gerrit
>




Dec 9 '06 #10
What you could do is create a static ApplicationContext class with a
CurrentUser property and then delegate the call to the
Thread.CurrentPrincipal.Identity

Gabriel Lozano-Morán

"Dave Sexton" <dave@jwa[remove.this]online.comwrote in message
news:eM**************@TK2MSFTNGP03.phx.gbl...
Hi Gabriel,

Well it could be useful in certain circumstances, I was just suggesting
that an OO approach is much easier to use and understand.

Actually, I'm using that in a project I'm working on right now (although
I've implemented IIdentity in a strong-Typed DataRow). What I realized
after doing that is it was pointless. I ended up using my static Program
class to aggregate a bunch of static info for the runtime and I realized
that adding a CurrentUser property was much simpler, so I use
Program.CurrentUser in my app now. That's much nicer than
Thread.CurrentPrincipal.Identity.Name, don't you agree?

--
Dave Sexton

"Gabriel Lozano-Morán" <ab***@frontbridge.comwrote in message
news:ul**************@TK2MSFTNGP05.phx.gbl...
>Can someone please explain me why this is not the most logical solution?
He clearly says that the user has to logon. Is there any better solution
than to use a generic principal object?

Beside the fact that it violates the Law of Demeter I don't see what is
wrong with calling System.Threading.Thread.CurrentPrincipal.Identity. Name

Gabriel Lozano-Morán
"Cor Ligthert [MVP]" <no************@planet.nlwrote in message
news:ec**************@TK2MSFTNGP02.phx.gbl...
>>Dave,

I agree with you that the CurrentPrincipal use is not the most logical.

I was more pointing on the inbuild security system.

Cor

"Dave Sexton" <dave@jwa[remove.this]online.comschreef in bericht
news:%2******************@TK2MSFTNGP03.phx.gbl.. .
Hi Cor,

I think a more OO approach is better. If the OP wants, there is no
reason why the User object can't just implement IIdentity as well,
although I think

User.CurrentUser.Name

is much more legible than

System.Threading.Thread.CurrentPrincipal.Identity. Name

so I don't know why anyone would prefer the latter :)

--
Dave Sexton

"Cor Ligthert [MVP]" <no************@planet.nlwrote in message
news:uM**************@TK2MSFTNGP04.phx.gbl...
Gerrit,
>
I go for the answer from Gabriel, but just a little addition to your
own code. I would not use the name Session in this way. It is an
attribute in ASPNET what is almost a keyword.
>
Cor
>
"Gerrit" <gs*************@hotmail.comschreef in bericht
news:c1***************************@news.chello .nl...
>Hallo,
>>
>In my application, the user must logon with a username and a
>password. In all the tables in my database, there is a field user. By
>saving new or changed records, the username is writed to that field.
>>
>Now I have a class Session with the User-information.
>By opening the program, I have code like this on the mainform:
>>
>Session sess = new Session().
>sess.UserName = "John";
>>
>When I open a new form, then I do this:
>>
>frmNew frm = new frmNew(sess);
>>
>When I write to the Database, I do this:
>>
>Article art = new Article(sess);
>>
>etc.
>>
>This is working, but I was asking by myself, that maybe there must be
>another way to do this.
>That I can store the information in a public class or struct and that
>I can read this from every other form or class in my program.
>>
>Is there another way? And how can I do that?
>thank you
>Gerrit
>>
>
>




Dec 9 '06 #11
Hi Gabriel,

That's almost what I've done already, but what value does the Identity
property provide if I already have a business object?

My code looks like this (basically):

static class Program
{
// I'm using a strong-Typed DataRow (as I mentioned before)
public static ProgramData.UsersRow CurrentUser
{
get { return user; }
set { user = value; }
}

private static ProgramData.UsersRow user;
}

It doesn't get much simpler than that :)

The fact that ProgramData.UsersRow implements IIdentity and is assigned to
the current principal doesn't help me at all, although I've left that code
in place as well out of laziness.

--
Dave Sexton

"Gabriel Lozano-Morán" <ab***@frontbridge.comwrote in message
news:uV**************@TK2MSFTNGP03.phx.gbl...
What you could do is create a static ApplicationContext class with a
CurrentUser property and then delegate the call to the
Thread.CurrentPrincipal.Identity

Gabriel Lozano-Morán

"Dave Sexton" <dave@jwa[remove.this]online.comwrote in message
news:eM**************@TK2MSFTNGP03.phx.gbl...
>Hi Gabriel,

Well it could be useful in certain circumstances, I was just suggesting
that an OO approach is much easier to use and understand.

Actually, I'm using that in a project I'm working on right now (although
I've implemented IIdentity in a strong-Typed DataRow). What I realized
after doing that is it was pointless. I ended up using my static Program
class to aggregate a bunch of static info for the runtime and I realized
that adding a CurrentUser property was much simpler, so I use
Program.CurrentUser in my app now. That's much nicer than
Thread.CurrentPrincipal.Identity.Name, don't you agree?

--
Dave Sexton

"Gabriel Lozano-Morán" <ab***@frontbridge.comwrote in message
news:ul**************@TK2MSFTNGP05.phx.gbl...
>>Can someone please explain me why this is not the most logical solution?
He clearly says that the user has to logon. Is there any better solution
than to use a generic principal object?

Beside the fact that it violates the Law of Demeter I don't see what is
wrong with calling
System.Threading.Thread.CurrentPrincipal.Identit y.Name

Gabriel Lozano-Morán
"Cor Ligthert [MVP]" <no************@planet.nlwrote in message
news:ec**************@TK2MSFTNGP02.phx.gbl...
Dave,

I agree with you that the CurrentPrincipal use is not the most logical.

I was more pointing on the inbuild security system.

Cor

"Dave Sexton" <dave@jwa[remove.this]online.comschreef in bericht
news:%2******************@TK2MSFTNGP03.phx.gbl. ..
Hi Cor,
>
I think a more OO approach is better. If the OP wants, there is no
reason why the User object can't just implement IIdentity as well,
although I think
>
User.CurrentUser.Name
>
is much more legible than
>
System.Threading.Thread.CurrentPrincipal.Identity. Name
>
so I don't know why anyone would prefer the latter :)
>
--
Dave Sexton
>
"Cor Ligthert [MVP]" <no************@planet.nlwrote in message
news:uM**************@TK2MSFTNGP04.phx.gbl.. .
>Gerrit,
>>
>I go for the answer from Gabriel, but just a little addition to your
>own code. I would not use the name Session in this way. It is an
>attribute in ASPNET what is almost a keyword.
>>
>Cor
>>
>"Gerrit" <gs*************@hotmail.comschreef in bericht
>news:c1***************************@news.chell o.nl...
>>Hallo,
>>>
>>In my application, the user must logon with a username and a
>>password. In all the tables in my database, there is a field user.
>>By saving new or changed records, the username is writed to that
>>field.
>>>
>>Now I have a class Session with the User-information.
>>By opening the program, I have code like this on the mainform:
>>>
>>Session sess = new Session().
>>sess.UserName = "John";
>>>
>>When I open a new form, then I do this:
>>>
>>frmNew frm = new frmNew(sess);
>>>
>>When I write to the Database, I do this:
>>>
>>Article art = new Article(sess);
>>>
>>etc.
>>>
>>This is working, but I was asking by myself, that maybe there must
>>be another way to do this.
>>That I can store the information in a public class or struct and
>>that I can read this from every other form or class in my program.
>>>
>>Is there another way? And how can I do that?
>>thank you
>>Gerrit
>>>
>>
>>
>
>




Dec 9 '06 #12
Garbriel,

I need in my eyes to much instructions using the principal that is all.
To much words for often done things, that is all.
(It should be done easier in my opinion, however that was again not about
your solution)

Cor

"Gabriel Lozano-Morán" <ab***@frontbridge.comschreef in bericht
news:ul**************@TK2MSFTNGP05.phx.gbl...
Can someone please explain me why this is not the most logical solution?
He clearly says that the user has to logon. Is there any better solution
than to use a generic principal object?

Beside the fact that it violates the Law of Demeter I don't see what is
wrong with calling System.Threading.Thread.CurrentPrincipal.Identity. Name

Gabriel Lozano-Morán
"Cor Ligthert [MVP]" <no************@planet.nlwrote in message
news:ec**************@TK2MSFTNGP02.phx.gbl...
>Dave,

I agree with you that the CurrentPrincipal use is not the most logical.

I was more pointing on the inbuild security system.

Cor

"Dave Sexton" <dave@jwa[remove.this]online.comschreef in bericht
news:%2******************@TK2MSFTNGP03.phx.gbl. ..
>>Hi Cor,

I think a more OO approach is better. If the OP wants, there is no
reason why the User object can't just implement IIdentity as well,
although I think

User.CurrentUser.Name

is much more legible than

System.Threading.Thread.CurrentPrincipal.Identity. Name

so I don't know why anyone would prefer the latter :)

--
Dave Sexton

"Cor Ligthert [MVP]" <no************@planet.nlwrote in message
news:uM**************@TK2MSFTNGP04.phx.gbl...
Gerrit,

I go for the answer from Gabriel, but just a little addition to your
own code. I would not use the name Session in this way. It is an
attribute in ASPNET what is almost a keyword.

Cor

"Gerrit" <gs*************@hotmail.comschreef in bericht
news:c1***************************@news.chello. nl...
Hallo,
>
In my application, the user must logon with a username and a password.
In all the tables in my database, there is a field user. By saving new
or changed records, the username is writed to that field.
>
Now I have a class Session with the User-information.
By opening the program, I have code like this on the mainform:
>
Session sess = new Session().
sess.UserName = "John";
>
When I open a new form, then I do this:
>
frmNew frm = new frmNew(sess);
>
When I write to the Database, I do this:
>
Article art = new Article(sess);
>
etc.
>
This is working, but I was asking by myself, that maybe there must be
another way to do this.
That I can store the information in a public class or struct and that
I can read this from every other form or class in my program.
>
Is there another way? And how can I do that?
thank you
Gerrit
>




Dec 9 '06 #13
Hello Dave,

The added value is that you don't have to reinvent the wheel as the current
infrastructure (Base Class Library) already provides a mechanism to solve
your problem in the form of Principals and Identities. But as always there
is never only one good solution to a problem.

Gabriel Lozano-Morán
Hi Gabriel,

That's almost what I've done already, but what value does the Identity
property provide if I already have a business object?

My code looks like this (basically):

static class Program
{
// I'm using a strong-Typed DataRow (as I mentioned before)
public static ProgramData.UsersRow CurrentUser
{
get { return user; }
set { user = value; }
}
private static ProgramData.UsersRow user;
}
It doesn't get much simpler than that :)

The fact that ProgramData.UsersRow implements IIdentity and is
assigned to the current principal doesn't help me at all, although
I've left that code in place as well out of laziness.

"Gabriel Lozano-Morán" <ab***@frontbridge.comwrote in message
news:uV**************@TK2MSFTNGP03.phx.gbl...
>What you could do is create a static ApplicationContext class with a
CurrentUser property and then delegate the call to the
Thread.CurrentPrincipal.Identity

Gabriel Lozano-Morán

"Dave Sexton" <dave@jwa[remove.this]online.comwrote in message
news:eM**************@TK2MSFTNGP03.phx.gbl...
>>Hi Gabriel,

Well it could be useful in certain circumstances, I was just
suggesting that an OO approach is much easier to use and understand.

Actually, I'm using that in a project I'm working on right now
(although I've implemented IIdentity in a strong-Typed DataRow).
What I realized after doing that is it was pointless. I ended up
using my static Program class to aggregate a bunch of static info
for the runtime and I realized that adding a CurrentUser property
was much simpler, so I use Program.CurrentUser in my app now.
That's much nicer than Thread.CurrentPrincipal.Identity.Name, don't
you agree?

-- Dave Sexton

"Gabriel Lozano-Morán" <ab***@frontbridge.comwrote in message
news:ul**************@TK2MSFTNGP05.phx.gbl...

Can someone please explain me why this is not the most logical
solution? He clearly says that the user has to logon. Is there any
better solution than to use a generic principal object?

Beside the fact that it violates the Law of Demeter I don't see
what is wrong with calling
System.Threading.Thread.CurrentPrincipal.Identi ty.Name

Gabriel Lozano-Morán

"Cor Ligthert [MVP]" <no************@planet.nlwrote in message
news:ec**************@TK2MSFTNGP02.phx.gbl...

Dave,
>
I agree with you that the CurrentPrincipal use is not the most
logical.
>
I was more pointing on the inbuild security system.
>
Cor
>
"Dave Sexton" <dave@jwa[remove.this]online.comschreef in bericht
news:%2******************@TK2MSFTNGP03.phx.gbl ...
>
>Hi Cor,
>>
>I think a more OO approach is better. If the OP wants, there is
>no reason why the User object can't just implement IIdentity as
>well, although I think
>>
>User.CurrentUser.Name
>>
>is much more legible than
>>
>System.Threading.Thread.CurrentPrincipal.Iden tity.Name
>>
>so I don't know why anyone would prefer the latter :)
>>
>-- Dave Sexton
>>
>"Cor Ligthert [MVP]" <no************@planet.nlwrote in message
>news:uM**************@TK2MSFTNGP04.phx.gbl. ..
>>
>>Gerrit,
>>>
>>I go for the answer from Gabriel, but just a little addition to
>>your own code. I would not use the name Session in this way. It
>>is an attribute in ASPNET what is almost a keyword.
>>>
>>Cor
>>>
>>"Gerrit" <gs*************@hotmail.comschreef in bericht
>>news:c1***************************@news.chel lo.nl...
>>>
>>>Hallo,
>>>>
>>>In my application, the user must logon with a username and a
>>>password. In all the tables in my database, there is a field
>>>user. By saving new or changed records, the username is writed
>>>to that field.
>>>>
>>>Now I have a class Session with the User-information. By
>>>opening the program, I have code like this on the mainform:
>>>>
>>>Session sess = new Session().
>>>sess.UserName = "John";
>>>When I open a new form, then I do this:
>>>>
>>>frmNew frm = new frmNew(sess);
>>>>
>>>When I write to the Database, I do this:
>>>>
>>>Article art = new Article(sess);
>>>>
>>>etc.
>>>>
>>>This is working, but I was asking by myself, that maybe there
>>>must
>>>be another way to do this.
>>>That I can store the information in a public class or struct
>>>and
>>>that I can read this from every other form or class in my
>>>program.
>>>Is there another way? And how can I do that?
>>>thank you
>>>Gerrit

Dec 9 '06 #14
Hi Gabriel,

It doesn't solve my problem. As you may recall, I tried your approach a
while back but realized soon that it wasn't enough and was more like I was
trying to use an FCL class just because it is an FCL class :)

The OP's User object will most likely add functionality that IIdentity
doesn't provide, such as CRUD operations or state such as a user's ID,
FirstName, LastName, etc - you know, the stuff most real-world applications
require, including the one I'm working on now. Implementing IIdentity and
using some abstract threading principal was just extra work for me. As I
already mentioned, it could be useful (e.g., if a third-party library uses
the currently logged on user's name for something), but within the context
of my own application it just doesn't provide any real value.

If some sort of roles framework is being used, as present in my current
project, then going through the principal is actually taking the long way to
get them. I'm using a custom DAL and DataSets (being partial in 2.0) are
perfect business entities for me. I've extended UsersRow with the following
code:

/// <summary>Gets a read-only copy of the user's roles.</summary>
public List<stringRoles
{
get
{
List<stringroles = new List<string>();

foreach (UserRolesRow role in GetUserRolesRows())
roles.Add(role.Role);

return roles;
}
}

My custom Users and Roles infrastructure allows me to do this:

Program.CurrentUser.Roles.Contains("admin");

and

Program.User.Roles.FindAll(delegate(string role) {
return role == "admin" || role == "manager";
});

--
Dave Sexton

"Gabriel Lozano-Morán" <ab***@frontbridge.comwrote in message
news:e9**************************@news.microsoft.c om...
Hello Dave,

The added value is that you don't have to reinvent the wheel as the
current infrastructure (Base Class Library) already provides a mechanism
to solve your problem in the form of Principals and Identities. But as
always there is never only one good solution to a problem.

Gabriel Lozano-Morán
>Hi Gabriel,

That's almost what I've done already, but what value does the Identity
property provide if I already have a business object?

My code looks like this (basically):

static class Program
{
// I'm using a strong-Typed DataRow (as I mentioned before)
public static ProgramData.UsersRow CurrentUser
{
get { return user; }
set { user = value; }
}
private static ProgramData.UsersRow user;
}
It doesn't get much simpler than that :)

The fact that ProgramData.UsersRow implements IIdentity and is
assigned to the current principal doesn't help me at all, although
I've left that code in place as well out of laziness.

"Gabriel Lozano-Morán" <ab***@frontbridge.comwrote in message
news:uV**************@TK2MSFTNGP03.phx.gbl...
>>What you could do is create a static ApplicationContext class with a
CurrentUser property and then delegate the call to the
Thread.CurrentPrincipal.Identity

Gabriel Lozano-Morán

"Dave Sexton" <dave@jwa[remove.this]online.comwrote in message
news:eM**************@TK2MSFTNGP03.phx.gbl...

Hi Gabriel,

Well it could be useful in certain circumstances, I was just
suggesting that an OO approach is much easier to use and understand.

Actually, I'm using that in a project I'm working on right now
(although I've implemented IIdentity in a strong-Typed DataRow).
What I realized after doing that is it was pointless. I ended up
using my static Program class to aggregate a bunch of static info
for the runtime and I realized that adding a CurrentUser property
was much simpler, so I use Program.CurrentUser in my app now.
That's much nicer than Thread.CurrentPrincipal.Identity.Name, don't
you agree?

-- Dave Sexton

"Gabriel Lozano-Morán" <ab***@frontbridge.comwrote in message
news:ul**************@TK2MSFTNGP05.phx.gbl...

Can someone please explain me why this is not the most logical
solution? He clearly says that the user has to logon. Is there any
better solution than to use a generic principal object?
>
Beside the fact that it violates the Law of Demeter I don't see
what is wrong with calling
System.Threading.Thread.CurrentPrincipal.Ident ity.Name
>
Gabriel Lozano-Morán
>
"Cor Ligthert [MVP]" <no************@planet.nlwrote in message
news:ec**************@TK2MSFTNGP02.phx.gbl.. .
>
>Dave,
>>
>I agree with you that the CurrentPrincipal use is not the most
>logical.
>>
>I was more pointing on the inbuild security system.
>>
>Cor
>>
>"Dave Sexton" <dave@jwa[remove.this]online.comschreef in bericht
>news:%2******************@TK2MSFTNGP03.phx.gb l...
>>
>>Hi Cor,
>>>
>>I think a more OO approach is better. If the OP wants, there is
>>no reason why the User object can't just implement IIdentity as
>>well, although I think
>>>
>>User.CurrentUser.Name
>>>
>>is much more legible than
>>>
>>System.Threading.Thread.CurrentPrincipal.Ide ntity.Name
>>>
>>so I don't know why anyone would prefer the latter :)
>>>
>>-- Dave Sexton
>>>
>>"Cor Ligthert [MVP]" <no************@planet.nlwrote in message
>>news:uM**************@TK2MSFTNGP04.phx.gbl.. .
>>>
>>>Gerrit,
>>>>
>>>I go for the answer from Gabriel, but just a little addition to
>>>your own code. I would not use the name Session in this way. It
>>>is an attribute in ASPNET what is almost a keyword.
>>>>
>>>Cor
>>>>
>>>"Gerrit" <gs*************@hotmail.comschreef in bericht
>>>news:c1***************************@news.che llo.nl...
>>>>
>>>>Hallo,
>>>>>
>>>>In my application, the user must logon with a username and a
>>>>password. In all the tables in my database, there is a field
>>>>user. By saving new or changed records, the username is writed
>>>>to that field.
>>>>>
>>>>Now I have a class Session with the User-information. By
>>>>opening the program, I have code like this on the mainform:
>>>>>
>>>>Session sess = new Session().
>>>>sess.UserName = "John";
>>>>When I open a new form, then I do this:
>>>>>
>>>>frmNew frm = new frmNew(sess);
>>>>>
>>>>When I write to the Database, I do this:
>>>>>
>>>>Article art = new Article(sess);
>>>>>
>>>>etc.
>>>>>
>>>>This is working, but I was asking by myself, that maybe there
>>>>must
>>>>be another way to do this.
>>>>That I can store the information in a public class or struct
>>>>and
>>>>that I can read this from every other form or class in my
>>>>program.
>>>>Is there another way? And how can I do that?
>>>>thank you
>>>>Gerrit


Dec 9 '06 #15
Hello Dave,

You could also create your own principal and identity objects and when you
need the principal object from the thread you can cast it to your custom
principal. So you could add the extra functionality to these classes and
in make the type of ApplicationContext.CurrentUser MyIdentity and then in
the getter write something like:

return (MyIdentity) Thread.CurrentPrincipal.Identity;

Gabriel Lozano-Morán
The .NET Aficionado
http://www.pointerx.net
Hi Gabriel,

It doesn't solve my problem. As you may recall, I tried your approach
a while back but realized soon that it wasn't enough and was more like
I was trying to use an FCL class just because it is an FCL class :)

The OP's User object will most likely add functionality that IIdentity
doesn't provide, such as CRUD operations or state such as a user's ID,
FirstName, LastName, etc - you know, the stuff most real-world
applications require, including the one I'm working on now.
Implementing IIdentity and using some abstract threading principal was
just extra work for me. As I already mentioned, it could be useful
(e.g., if a third-party library uses the currently logged on user's
name for something), but within the context of my own application it
just doesn't provide any real value.

If some sort of roles framework is being used, as present in my
current project, then going through the principal is actually taking
the long way to get them. I'm using a custom DAL and DataSets (being
partial in 2.0) are perfect business entities for me. I've extended
UsersRow with the following code:

/// <summary>Gets a read-only copy of the user's roles.</summary>
public List<stringRoles
{
get
{
List<stringroles = new List<string>();
foreach (UserRolesRow role in GetUserRolesRows())
roles.Add(role.Role);
return roles;
}
}
My custom Users and Roles infrastructure allows me to do this:

Program.CurrentUser.Roles.Contains("admin");

and

Program.User.Roles.FindAll(delegate(string role) {
return role == "admin" || role == "manager";
});
"Gabriel Lozano-Morán" <ab***@frontbridge.comwrote in message
news:e9**************************@news.microsoft.c om...
>Hello Dave,

The added value is that you don't have to reinvent the wheel as the
current infrastructure (Base Class Library) already provides a
mechanism to solve your problem in the form of Principals and
Identities. But as always there is never only one good solution to a
problem.

Gabriel Lozano-Morán
>>Hi Gabriel,

That's almost what I've done already, but what value does the
Identity property provide if I already have a business object?

My code looks like this (basically):

static class Program
{
// I'm using a strong-Typed DataRow (as I mentioned before)
public static ProgramData.UsersRow CurrentUser
{
get { return user; }
set { user = value; }
}
private static ProgramData.UsersRow user;
}
It doesn't get much simpler than that :)
The fact that ProgramData.UsersRow implements IIdentity and is
assigned to the current principal doesn't help me at all, although
I've left that code in place as well out of laziness.

"Gabriel Lozano-Morán" <ab***@frontbridge.comwrote in message
news:uV**************@TK2MSFTNGP03.phx.gbl...

What you could do is create a static ApplicationContext class with
a CurrentUser property and then delegate the call to the
Thread.CurrentPrincipal.Identity

Gabriel Lozano-Morán

"Dave Sexton" <dave@jwa[remove.this]online.comwrote in message
news:eM**************@TK2MSFTNGP03.phx.gbl...

Hi Gabriel,
>
Well it could be useful in certain circumstances, I was just
suggesting that an OO approach is much easier to use and
understand.
>
Actually, I'm using that in a project I'm working on right now
(although I've implemented IIdentity in a strong-Typed DataRow).
What I realized after doing that is it was pointless. I ended up
using my static Program class to aggregate a bunch of static info
for the runtime and I realized that adding a CurrentUser property
was much simpler, so I use Program.CurrentUser in my app now.
That's much nicer than Thread.CurrentPrincipal.Identity.Name,
don't you agree?
>
-- Dave Sexton
>
"Gabriel Lozano-Morán" <ab***@frontbridge.comwrote in message
news:ul**************@TK2MSFTNGP05.phx.gbl.. .
>
>Can someone please explain me why this is not the most logical
>solution? He clearly says that the user has to logon. Is there
>any better solution than to use a generic principal object?
>>
>Beside the fact that it violates the Law of Demeter I don't see
>what is wrong with calling
>System.Threading.Thread.CurrentPrincipal.Iden tity.Name
>>
>Gabriel Lozano-Morán
>>
>"Cor Ligthert [MVP]" <no************@planet.nlwrote in message
>news:ec**************@TK2MSFTNGP02.phx.gbl. ..
>>
>>Dave,
>>>
>>I agree with you that the CurrentPrincipal use is not the most
>>logical.
>>>
>>I was more pointing on the inbuild security system.
>>>
>>Cor
>>>
>>"Dave Sexton" <dave@jwa[remove.this]online.comschreef in
>>bericht news:%2******************@TK2MSFTNGP03.phx.gbl...
>>>
>>>Hi Cor,
>>>>
>>>I think a more OO approach is better. If the OP wants, there
>>>is no reason why the User object can't just implement IIdentity
>>>as well, although I think
>>>>
>>>User.CurrentUser.Name
>>>>
>>>is much more legible than
>>>>
>>>System.Threading.Thread.CurrentPrincipal.Id entity.Name
>>>>
>>>so I don't know why anyone would prefer the latter :)
>>>>
>>>-- Dave Sexton
>>>>
>>>"Cor Ligthert [MVP]" <no************@planet.nlwrote in
>>>message news:uM**************@TK2MSFTNGP04.phx.gbl...
>>>>
>>>>Gerrit,
>>>>>
>>>>I go for the answer from Gabriel, but just a little addition
>>>>to your own code. I would not use the name Session in this
>>>>way. It is an attribute in ASPNET what is almost a keyword.
>>>>>
>>>>Cor
>>>>>
>>>>"Gerrit" <gs*************@hotmail.comschreef in bericht
>>>>news:c1***************************@news.ch ello.nl...
>>>>>
>>>>>Hallo,
>>>>>>
>>>>>In my application, the user must logon with a username and a
>>>>>password. In all the tables in my database, there is a field
>>>>>user. By saving new or changed records, the username is
>>>>>writed to that field.
>>>>>>
>>>>>Now I have a class Session with the User-information. By
>>>>>opening the program, I have code like this on the mainform:
>>>>>>
>>>>>Session sess = new Session().
>>>>>sess.UserName = "John";
>>>>>When I open a new form, then I do this:
>>>>>frmNew frm = new frmNew(sess);
>>>>>>
>>>>>When I write to the Database, I do this:
>>>>>>
>>>>>Article art = new Article(sess);
>>>>>>
>>>>>etc.
>>>>>>
>>>>>This is working, but I was asking by myself, that maybe there
>>>>>must
>>>>>be another way to do this.
>>>>>That I can store the information in a public class or struct
>>>>>and
>>>>>that I can read this from every other form or class in my
>>>>>program.
>>>>>Is there another way? And how can I do that?
>>>>>thank you
>>>>>Gerrit

Dec 9 '06 #16
Hi Gabriel,

Correction:
Program.User.Roles.FindAll(delegate(string role) {
return role == "admin" || role == "manager";
});
The above should use Program.CurrentUser to remain consistent with my other
examples.

Actually, it's just Program.User in my real application (I copied and pasted
the code and changed the role literals) but I wanted it to be clear to the
OP so I used CurrentUser in my examples :)

--
Dave Sexton

"Dave Sexton" <dave@jwa[remove.this]online.comwrote in message
news:er**************@TK2MSFTNGP06.phx.gbl...
Hi Gabriel,

It doesn't solve my problem. As you may recall, I tried your approach a
while back but realized soon that it wasn't enough and was more like I was
trying to use an FCL class just because it is an FCL class :)

The OP's User object will most likely add functionality that IIdentity
doesn't provide, such as CRUD operations or state such as a user's ID,
FirstName, LastName, etc - you know, the stuff most real-world
applications require, including the one I'm working on now. Implementing
IIdentity and using some abstract threading principal was just extra work
for me. As I already mentioned, it could be useful (e.g., if a
third-party library uses the currently logged on user's name for
something), but within the context of my own application it just doesn't
provide any real value.

If some sort of roles framework is being used, as present in my current
project, then going through the principal is actually taking the long way
to get them. I'm using a custom DAL and DataSets (being partial in 2.0)
are perfect business entities for me. I've extended UsersRow with the
following code:

/// <summary>Gets a read-only copy of the user's roles.</summary>
public List<stringRoles
{
get
{
List<stringroles = new List<string>();

foreach (UserRolesRow role in GetUserRolesRows())
roles.Add(role.Role);

return roles;
}
}

My custom Users and Roles infrastructure allows me to do this:

Program.CurrentUser.Roles.Contains("admin");

and

Program.User.Roles.FindAll(delegate(string role) {
return role == "admin" || role == "manager";
});

--
Dave Sexton

"Gabriel Lozano-Morán" <ab***@frontbridge.comwrote in message
news:e9**************************@news.microsoft.c om...
>Hello Dave,

The added value is that you don't have to reinvent the wheel as the
current infrastructure (Base Class Library) already provides a mechanism
to solve your problem in the form of Principals and Identities. But as
always there is never only one good solution to a problem.

Gabriel Lozano-Morán
>>Hi Gabriel,

That's almost what I've done already, but what value does the Identity
property provide if I already have a business object?

My code looks like this (basically):

static class Program
{
// I'm using a strong-Typed DataRow (as I mentioned before)
public static ProgramData.UsersRow CurrentUser
{
get { return user; }
set { user = value; }
}
private static ProgramData.UsersRow user;
}
It doesn't get much simpler than that :)

The fact that ProgramData.UsersRow implements IIdentity and is
assigned to the current principal doesn't help me at all, although
I've left that code in place as well out of laziness.

"Gabriel Lozano-Morán" <ab***@frontbridge.comwrote in message
news:uV**************@TK2MSFTNGP03.phx.gbl...

What you could do is create a static ApplicationContext class with a
CurrentUser property and then delegate the call to the
Thread.CurrentPrincipal.Identity

Gabriel Lozano-Morán

"Dave Sexton" <dave@jwa[remove.this]online.comwrote in message
news:eM**************@TK2MSFTNGP03.phx.gbl...

Hi Gabriel,
>
Well it could be useful in certain circumstances, I was just
suggesting that an OO approach is much easier to use and understand.
>
Actually, I'm using that in a project I'm working on right now
(although I've implemented IIdentity in a strong-Typed DataRow).
What I realized after doing that is it was pointless. I ended up
using my static Program class to aggregate a bunch of static info
for the runtime and I realized that adding a CurrentUser property
was much simpler, so I use Program.CurrentUser in my app now.
That's much nicer than Thread.CurrentPrincipal.Identity.Name, don't
you agree?
>
-- Dave Sexton
>
"Gabriel Lozano-Morán" <ab***@frontbridge.comwrote in message
news:ul**************@TK2MSFTNGP05.phx.gbl.. .
>
>Can someone please explain me why this is not the most logical
>solution? He clearly says that the user has to logon. Is there any
>better solution than to use a generic principal object?
>>
>Beside the fact that it violates the Law of Demeter I don't see
>what is wrong with calling
>System.Threading.Thread.CurrentPrincipal.Iden tity.Name
>>
>Gabriel Lozano-Morán
>>
>"Cor Ligthert [MVP]" <no************@planet.nlwrote in message
>news:ec**************@TK2MSFTNGP02.phx.gbl. ..
>>
>>Dave,
>>>
>>I agree with you that the CurrentPrincipal use is not the most
>>logical.
>>>
>>I was more pointing on the inbuild security system.
>>>
>>Cor
>>>
>>"Dave Sexton" <dave@jwa[remove.this]online.comschreef in bericht
>>news:%2******************@TK2MSFTNGP03.phx.g bl...
>>>
>>>Hi Cor,
>>>>
>>>I think a more OO approach is better. If the OP wants, there is
>>>no reason why the User object can't just implement IIdentity as
>>>well, although I think
>>>>
>>>User.CurrentUser.Name
>>>>
>>>is much more legible than
>>>>
>>>System.Threading.Thread.CurrentPrincipal.Id entity.Name
>>>>
>>>so I don't know why anyone would prefer the latter :)
>>>>
>>>-- Dave Sexton
>>>>
>>>"Cor Ligthert [MVP]" <no************@planet.nlwrote in message
>>>news:uM**************@TK2MSFTNGP04.phx.gbl. ..
>>>>
>>>>Gerrit,
>>>>>
>>>>I go for the answer from Gabriel, but just a little addition to
>>>>your own code. I would not use the name Session in this way. It
>>>>is an attribute in ASPNET what is almost a keyword.
>>>>>
>>>>Cor
>>>>>
>>>>"Gerrit" <gs*************@hotmail.comschreef in bericht
>>>>news:c1***************************@news.ch ello.nl...
>>>>>
>>>>>Hallo,
>>>>>>
>>>>>In my application, the user must logon with a username and a
>>>>>password. In all the tables in my database, there is a field
>>>>>user. By saving new or changed records, the username is writed
>>>>>to that field.
>>>>>>
>>>>>Now I have a class Session with the User-information. By
>>>>>opening the program, I have code like this on the mainform:
>>>>>>
>>>>>Session sess = new Session().
>>>>>sess.UserName = "John";
>>>>>When I open a new form, then I do this:
>>>>>>
>>>>>frmNew frm = new frmNew(sess);
>>>>>>
>>>>>When I write to the Database, I do this:
>>>>>>
>>>>>Article art = new Article(sess);
>>>>>>
>>>>>etc.
>>>>>>
>>>>>This is working, but I was asking by myself, that maybe there
>>>>>must
>>>>>be another way to do this.
>>>>>That I can store the information in a public class or struct
>>>>>and
>>>>>that I can read this from every other form or class in my
>>>>>program.
>>>>>Is there another way? And how can I do that?
>>>>>thank you
>>>>>Gerrit



Dec 9 '06 #17
Hi Gabriel,
You could also create your own principal and identity objects and when you
need the principal object from the thread you can cast it to your custom
principal. So you could add the extra functionality to these classes and
in make the type of ApplicationContext.CurrentUser MyIdentity and then in
the getter write something like:

return (MyIdentity) Thread.CurrentPrincipal.Identity;
Well, I could, but there is no need to do that, nor will it provide anything
useful over my current solution where I simply return "user".

BTW, I prefer using the Program class - I'm not looking for another solution
here - I'm using a custom framework that I wrote named, "AppBase", and my
Program class actually derives from a base class named,
"ApplicationManager", so it's not actually static. My infrastructure is
already in place and has been working nicely for me for about 2 years now,
and there has never been a need or desire to use a thread principal at all
since I switched to using a simple static field.

I prefer my code simply because it's more legible, doesn't require any
interface implementation, doesn't require using a BCL infrastructure under
my custom business objects anyway, and it's really easy to understand.

It looks like we're just going to have to disagree on this :)

--
Dave Sexton
Dec 9 '06 #18

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Tom Leylan | last post by:
I'd like to find/have a discussion about the various ways an application server could be implemented. So the upsides and downsides can be discussed. In case I'm using the term wrong by "app...
42
by: Dan | last post by:
Hello, I have trouble with class calling. I am calling getvolume() with succes in the function CreateCircle but it do not want to call it in ShowCircle() function. I am staying in the same...
10
by: Ray Z | last post by:
hi, there I build a Class Library, I write a class A to implement interface IA. The problem is when I give the dll file to others, thet can get all information about not only IA, but also A. They...
8
by: Elliot M. Rodriguez | last post by:
I am having a heckuva time debugging this, or determining why my page is behaving this way. I have a search form, that when completed, returns a datagrid. When the user selects a row (normal...
9
by: Microsoft News Server | last post by:
Hi, I am currently having a problem with random, intermittent lock ups in my ASP.net application on our production server (99% CPU usage by 3 threads, indefinately). I currently use IIS Debug...
3
by: Minh Khoa | last post by:
Please give me more information about delegate and its usage? Why do i use it and when?
3
by: Gladys | last post by:
Dear newsgroup - do not reply to this e-mail address. I have just created it to avoid getting spam to my real address. Reply to the newsgroup. I am watching them all now so please reply now. ...
5
by: =?Utf-8?B?R3JhbnQ=?= | last post by:
Hi, In a WinForms application, how can you get the identity of the remote credentials used when kicking off the application with RunAs /NetOnly? I can get local Identity information with...
12
by: raylopez99 | last post by:
Keywords: scope resolution, passing classes between parent and child forms, parameter constructor method, normal constructor, default constructor, forward reference, sharing classes between forms....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.