Print Page | Close Window

Authentication context and problem with entity manager

Printed From: IdeaBlade
Category: Cocktail
Forum Name: Community Forum
Forum Discription: A professional application framework using Caliburn.Micro and DevForce
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=3386
Printed Date: 30-Mar-2024 at 5:30am


Topic: Authentication context and problem with entity manager
Posted By: pponzano
Subject: Authentication context and problem with entity manager
Date Posted: 11-Apr-2012 at 12:07am
Hello,
I've got a console application on wich I use the IAuthenticationService to login... I correctly login and it saves the principal but then I call the entitymanager and execute a method I got an exception that the user is not logged in...

in my program.cs I've a configure method that's

   private void Configure()
        {
            IAuthenticationService authenticationService = IoC.Get<IAuthenticationService>();

            if (authenticationService != null)
            {
                string userName = ConfigurationManager.AppSettings["username"];
                string password = ConfigurationManager.AppSettings["password"];
                myLoginCredential loginCredential = new myLoginCredential(userName, password);

                authenticationService.Login(loginCredential); //here it logs and the AuthenticationContext is ok
            }
        }

When I call my repository

private bool PopulateSqlData(DateTime data)

       {

           bool isOk = false;

 

           var sw = new Stopwatch();

 

           sw.Start();

 

           try

           {

               repository.GeneratoreReportmyMethod(id, data);

           }

           catch (Exception ex)

           {

               NLog.LogManager.GetLogger("exception").ErrorException("PopulateSqlData", ex);

               return false;

           }

 

           sw.Stop();

           string str = string.Format("Tempo impiegato per popolare le tabelle dati {0}", sw.Elapsed.ToString());

 

           NLog.LogManager.GetLogger("generatore").Info(str);

           return isOk;

       }


I get a call to the loginmanager on the server


   public IPrincipal Login(ILoginCredential credential, EntityManager entityManager)
   {

//credential is null here

   }


In my old version using DAF I was calling LinkForAuthentication...what am I doing wrong?
Thanks



Replies:
Posted By: mgood
Date Posted: 11-Apr-2012 at 1:08am
Well, you are using Cocktail in a console application. Cocktail doesn't come with a bootstrapper that correctly composes a console app, so you are most likely missing many pieces trying to shoehorn Cocktail into a console application. 

In this particular case the EntityManager isn't getting the AuthenticationContext assigned, because the hook is missing somewhere. In a Silverlight or WPF application, which are the only two platforms officially supported by Cocktail, the FrameworkBootstrapper makes sure that every EntityManager gets the AuthenticationContext assigned. 

Modifying the Configure method like follows may get you a little further:

private void Configure()
        {
            IAuthenticationService authenticationService = IoC.Get<IAuthenticationService>();

   EntityManager.EntityManagerCreated += (sender, args) => args.EntityManager.AuthenticationContext = authenticationService.AuthenticationContext;

            if (authenticationService != null)
            {
                string userName = ConfigurationManager.AppSettings["username"];
                string password = ConfigurationManager.AppSettings["password"];
                myLoginCredential loginCredential = new myLoginCredential(userName, password);

                authenticationService.Login(loginCredential); //here it logs and the AuthenticationContext is ok
            }
        }



Print Page | Close Window