Home All Groups Group Topic Archive Search About

WinForm user authentication

Author
25 Oct 2006 5:57 PM
Diane Yocom
Can someone point me in the right direction for finding out how to do custom
authentication in a WinForm application?

I have a WinForm (VB.Net) application with a login screen.  I want my users
to input their username and password, then I will authenticate them against
my database.  That's fine, I can do that.  What I can't figure out is how to
now tell dotnet that My.User has been authenticated and what roles they are
in.

I'm converting this from an ASP.Net application, so I've created my own
CustomPrincipal class, I'm just missing how to say, "Yes, this user is
authenticated" and I can't figure out what to search on to give me any
useful information.

Help!
Diane

Author
25 Oct 2006 6:16 PM
Claus Konrad
Hi

You should use the GenericIdentity and GenericPrincipal objects for that.
And do associate the current identity to the current thread to be allowed to
detect who is calling your methods.

Example:

    class Program
    {
        static void Main(string[] args)
        {
            IIdentity iden = new GenericIdentity("Claus");
            string[] roles = new string[]{"Admin", "Power"};
            IPrincipal prin = new GenericPrincipal(iden, roles);

            System.Threading.Thread.CurrentPrincipal = prin;

            //call method
            MyMethod();

            Console.WriteLine("Done");
            Console.Read();
        }


        static void MyMethod()
        {
            //assert caller
            IIdentity caller = System.Threading.Thread.CurrentPrincipal.Identity;
            Console.WriteLine("Caller = {0}", caller.Name);

        }
    }
--
rgds.
/Claus Konrad
www.clauskonrad.net

Show quoteHide quote
"Diane Yocom" wrote:

> Can someone point me in the right direction for finding out how to do custom
> authentication in a WinForm application?
>
> I have a WinForm (VB.Net) application with a login screen.  I want my users
> to input their username and password, then I will authenticate them against
> my database.  That's fine, I can do that.  What I can't figure out is how to
> now tell dotnet that My.User has been authenticated and what roles they are
> in.
>
> I'm converting this from an ASP.Net application, so I've created my own
> CustomPrincipal class, I'm just missing how to say, "Yes, this user is
> authenticated" and I can't figure out what to search on to give me any
> useful information.
>
> Help!
> Diane
>
>
>
Author
25 Oct 2006 6:19 PM
Diane Yocom
Thanks, Claus.

I also found this in the help (finally - was just staring me in the face):
ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.VisualStudio.v80.en/dv_vbcn/html/c47b8c08-3ca9-46c4-b4b0-b06dd2b956f8.htm

Show quoteHide quote
"Claus Konrad" <ClausKon***@discussions.microsoft.com> wrote in message
news:2A628733-A2AA-40CF-9FE8-E35A957C31BE@microsoft.com...
> Hi
>
> You should use the GenericIdentity and GenericPrincipal objects for that.
> And do associate the current identity to the current thread to be allowed
> to
> detect who is calling your methods.
>
> Example:
>
> class Program
> {
> static void Main(string[] args)
> {
> IIdentity iden = new GenericIdentity("Claus");
> string[] roles = new string[]{"Admin", "Power"};
> IPrincipal prin = new GenericPrincipal(iden, roles);
>
> System.Threading.Thread.CurrentPrincipal = prin;
>
> //call method
> MyMethod();
>
> Console.WriteLine("Done");
> Console.Read();
> }
>
>
> static void MyMethod()
> {
> //assert caller
> IIdentity caller = System.Threading.Thread.CurrentPrincipal.Identity;
> Console.WriteLine("Caller = {0}", caller.Name);
>
> }
> }
> --
> rgds.
> /Claus Konrad
> www.clauskonrad.net
>
> "Diane Yocom" wrote:
>
>> Can someone point me in the right direction for finding out how to do
>> custom
>> authentication in a WinForm application?
>>
>> I have a WinForm (VB.Net) application with a login screen.  I want my
>> users
>> to input their username and password, then I will authenticate them
>> against
>> my database.  That's fine, I can do that.  What I can't figure out is how
>> to
>> now tell dotnet that My.User has been authenticated and what roles they
>> are
>> in.
>>
>> I'm converting this from an ASP.Net application, so I've created my own
>> CustomPrincipal class, I'm just missing how to say, "Yes, this user is
>> authenticated" and I can't figure out what to search on to give me any
>> useful information.
>>
>> Help!
>> Diane
>>
>>
>>
Author
25 Oct 2006 9:59 PM
Dominick Baier
maybe first of all you should ask yourself why you are building your own
auth/authZ system - Windows provides this for you already...

---
Dominick Baier, DevelopMentor
http://www.leastprivilege.com

Show quoteHide quote
> Hi
>
> You should use the GenericIdentity and GenericPrincipal objects for
> that.
> And do associate the current identity to the current thread to be
> allowed to
> detect who is calling your methods.
> Example:
>
> class Program
> {
> static void Main(string[] args)
> {
> IIdentity iden = new GenericIdentity("Claus");
> string[] roles = new string[]{"Admin", "Power"};
> IPrincipal prin = new GenericPrincipal(iden, roles);
> System.Threading.Thread.CurrentPrincipal = prin;
>
> //call method
> MyMethod();
> Console.WriteLine("Done");
> Console.Read();
> }
> static void MyMethod()
> {
> //assert caller
> IIdentity caller =
> System.Threading.Thread.CurrentPrincipal.Identity;
> Console.WriteLine("Caller = {0}", caller.Name);
> }
> }
> "Diane Yocom" wrote:
>> Can someone point me in the right direction for finding out how to do
>> custom authentication in a WinForm application?
>>
>> I have a WinForm (VB.Net) application with a login screen.  I want my
>> users to input their username and password, then I will authenticate
>> them against my database.  That's fine, I can do that.  What I can't
>> figure out is how to now tell dotnet that My.User has been
>> authenticated and what roles they are in.
>>
>> I'm converting this from an ASP.Net application, so I've created my
>> own CustomPrincipal class, I'm just missing how to say, "Yes, this
>> user is authenticated" and I can't figure out what to search on to
>> give me any useful information.
>>
>> Help!
>> Diane
Author
25 Oct 2006 11:11 PM
"Hadi Hariri" <>
Dominick Baier wrote:

> maybe first of all you should ask yourself why you are building your
> own auth/authZ system - Windows provides this for you already...
>

Maybe you want to have custom authentication and not rely on external
factors?