New Posts New Posts RSS Feed: EntityManager Authenticated+ DynamicXAP
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

EntityManager Authenticated+ DynamicXAP

 Post Reply Post Reply
Author
Molinari View Drop Down
Groupie
Groupie
Avatar

Joined: 25-Aug-2010
Posts: 42
Post Options Post Options   Quote Molinari Quote  Post ReplyReply Direct Link To This Post Topic: EntityManager Authenticated+ DynamicXAP
    Posted: 05-Jul-2011 at 12:34pm
I implemented for authentication (IEntityLoginManager)

    public class LoginManager : IEntityLoginManager
    {
        public IPrincipal Login(ILoginCredential credential, EntityManager entityManager)
        {
            //TODO
        }
     }


I have a project with three XAPs, when do I load the first XAP I call the pair LoginAsync, then I want to stay connected when the next XAP is loaded, but my EntityManager is null; then try to LoginAsync() again. but error below :


Type 'SilverlightProject.Customer' cannot be added to list of known types since another type 'SilverlightProject.Customer' with the same data contract name 'http://schemas.datacontract.org/2004/07/SilverlightProject:Customer' is already present.


it is possible to stay connected when I switch XAP ? or need LoginAsync again?

thanks
   
Back to Top
robertg View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 15-Mar-2011
Location: California
Posts: 87
Post Options Post Options   Quote robertg Quote  Post ReplyReply Direct Link To This Post Posted: 05-Jul-2011 at 3:21pm
Molinari,
 
You should be able to login separately in all three xaps with LoginAsync(), using the same credentials or individual credentials. Could you provide some more of your code? Are you really using the IEntityLoginManager implementation you posted above? Could I see how you're authenticating in your first xap (which works) and then your next xap (which is not)?
 
What block of code is actually creating the error you posted?
 
Thanks,
Robert
Back to Top
Molinari View Drop Down
Groupie
Groupie
Avatar

Joined: 25-Aug-2010
Posts: 42
Post Options Post Options   Quote Molinari Quote  Post ReplyReply Direct Link To This Post Posted: 06-Jul-2011 at 7:01am
Robert,

I would use the same credentials for login. (I would like to stay conected if possible, but if not possible then can be separately);


I use IEntityLoginManager :

public class LoginManager : IEntityLoginManager
    {
        public IPrincipal Login(ILoginCredential credential, EntityManager entityManager)
        {
            if (credential == null)
            {
               throw new LoginException(LoginExceptionType.NoCredentials, "No credentials were supplied");
            }

            UserPerson user = UserPerson.getUserByUserName(credential.UserName, entityManager);


            if (usuario == null)
            {
               throw new LoginException(LoginExceptionType.Other, "Error creating user login, try again.");
            }

            if (usuario.EntityAspect.IsNullEntity)
            {
               throw new LoginException(LoginExceptionType.InvalidUserName, "Invalid password and user name");
            }

            if (credential.Password != user.Password)
            {
               throw new LoginException(LoginExceptionType.InvalidPassword, "Invalid password and user name");
            }

            if (user.Active != "S")
            {
               throw new LoginException(LoginExceptionType.Other, "inactive User");
            }

            return user;
        }

        public void Logout(IPrincipal principal, EntityManager entityManager)
        {

        }

     }


// IPrincipal

public partial class PersonUser : IbEm.Entity, IPrincipal
    {

        public static PersonUser getUserByUserName(String UserName, EntityManager entityManager)
        {
            EntitiesTest _em = new EntitiesTest(entityManager);


            IEntityQuery<PersonUser> queryUserName = _em.PersonUser.Where(user => user.LOGIN == UserName).Include("Person").Include("PersonUserProfile");

            PersonUser _user = null;

            EventWaitHandle s_Signal = new ManualResetEvent(false);

            var op = queryUserName.ExecuteAsync();

            op.Completed += (obj, args) =>
            {
               if (!args.HasError)
               {
                    if (args.Results.Count() > 0)
                    {
                        _user = (PersonUser)args.Results.ToList().First();
                    }
                    else
                    {
                        _user = _em.GetNullEntity<PersonUser>();
                    }
               }

               if (s_Signal != null)
                    s_Signal.Set();
            };

            s_Signal.WaitOne();

            return _user;
        }

        public IIdentity Identity
        {
            get
            {
               String name = "";
               String type = "";

               if (this.Person.ContactPerson == "F")
               {
                    name = this.Person.Name;
                    type = "F";
               }
               else
               {
                    name = this.Person.Name + " / " + this.Person.NickName;
                    type = "J";
               }

               var identity = new UserIdentity(name, type, true);
               return identity;
            }
        }

        public bool IsInRole(string role)
        {
            return false;
        }
    }


here one button to login in first XAP>>>
//LOGIN PAGE

        EntitiesTest _em = new EntitiesTest();

        private void btnLogin_Click(object sender, RoutedEventArgs e)
        {
            LoginCredential credencial = new LoginCredential(txtLogin.Text, pwbSenha.Password, "");

            _em.LoginAsync(credencial, args =>
            {
               if (args.HasError)              
                    MessageBox.Show(args.Error.Message);              
               else
               {
                    Managers.Instance = _em;
                    PersonUser user = (_em.Principal as PersonUser);
                    NavigationService.Navigate(new Uri("/PrincipalPage.xaml", UriKind.Relative));
               }
            });
        }



here test code for downloadXAP "working perfect"

// Here PrincipalPage.xaml

      private void Button_Click(object sender, RoutedEventArgs e)
        {
            DoDownload("SilverlightProject.Processing.xap");
        }

      void DoDownload(string xapName)
        {
            var xap = new DynamicXap(new Uri(xapName, UriKind.Relative));
            xap.Loaded += (o, args) =>
            {
               if (args.HasError)              
                    MessageBox.Show(args.Error.Message);              
               else
               {
                    var dzap = o as DynamicXap;
                    IdeaBlade.Core.Composition.CompositionHost.Add(dzap);

                    CreatingObjeto(dzap.Assemblies.First());
               }
            };

      void CreatingObjeto(Assembly assembly)
        {
            try
            {
               Object dynLoadedObject = new object();

               dynLoadedObject = assembly.CreateInstance("SilverlightProject.Processing.MainPage");

               UIElement myControl = dynLoadedObject as UIElement;

               //add no component
               Container.Children.Clear();
               Container.ColumnDefinitions.RemoveAt(0);
               Container.Children.Add(myControl);
               Container.UpdateLayout();

            }
            catch (Exception ex)
            {
               MessageBox.Show(ex.InnerException.Message);
            }
        }

// final PrincipalPage.xaml


now the page tha brings me the error. second (MainPage) just one button for execute query and one datagrid

private void button1_Click(object sender, RoutedEventArgs e)
        {
            //EntitiesTest em = new EntitiesTest(Managers.Instance);

            //em.LoginAsync(new LoginCredential("c", "c", ""), args =>
            //    {
            //        if (args.CompletedSuccessfully)
            //        {
                        Managers.Instance.Customer.ExecuteAsync(op =>
                        {
                            if (op.CompletedSuccessfully)
                                dataGrid1.ItemsSource = op.Results;
                            else
                                MessageBox.Show(op.Error.Message);
                        });
               //    }
               //    else
               //        MessageBox.Show(args.Error.Message);
               //});            
        }

here examples tester.... on click button


1- i create class Managers and first login I get EM and set in Managers.Instance = EM, then i using here for create the new
            EntitiesTest em = new EntitiesTest(Managers.Instance); and execute my query with   em.Customer.ExecuteAsync(); OK. this not show error but dont show data. then i click button
again for load data, then throw my exception(   throw new LoginException(LoginExceptionType.NoCredentials, "No credentials were supplied"); )


2- now i picked up directly from the class Managers.Instance and execute the query Managers.Instance.Customer.ExecuteAsync(); here show same exception
(   throw new LoginException(LoginExceptionType.NoCredentials, "No credentials were supplied"); ) So i debug project Managers.Instance is null, but if i use EntitiesTest em = new Entities(Managers.Instance); <= my Instance not null;

3- I try loginAsync() after execute query, using Manager.Instance.LoginAsync(); or EntitiesTest em = new EntitiesTest(); em,LoginAsync(); ERROR
      ( Type 'SilverlightProject.Customer' cannot be added to list of known types since another type 'SilverlightProject.Customer' with the same data contract name 'http://schemas.datacontract.org/2004/07/SilverlightProject:Customer' is already present. )





here my class Managers


public class Managers
    {
        private static EntitiesTest _instance;

        public static EntitiesTest Instance
        {
            get
            {
               if (_instance == null)
                    _instance = new EntitiesTest();

               return Managers._instance;
            }
            set { Managers._instance = value; }
        }

    }


thanks.


Edited by Molinari - 07-Jul-2011 at 4:45am
Back to Top
robertg View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 15-Mar-2011
Location: California
Posts: 87
Post Options Post Options   Quote robertg Quote  Post ReplyReply Direct Link To This Post Posted: 06-Jul-2011 at 4:28pm
Thank you for sharing that code.
 
Based on what I'm looking at, I don't think that the error you're getting has anything to do with the multiple .xaps, but with the very convoluted way you're attempting to retrieve the data in that second .xap. You don't get the same error message if you simply create your em and log it in, do  you?
 
private void button1_Click(object sender, RoutedEventArgs e)
        { 
            EntitiesTest em = new EntitiesTest(Managers.Instance); 
            em.LoginAsync(new LoginCredential("c", "c", ""), args => 
            { 
              if (args.CompletedSuccessfully) 
             { 
                      // doing nothing here
              } 
              else 
                 MessageBox.Show(args.Error.Message); 
              });            
        }
What are you trying to accomplish with the Managers class?
Back to Top
Molinari View Drop Down
Groupie
Groupie
Avatar

Joined: 25-Aug-2010
Posts: 42
Post Options Post Options   Quote Molinari Quote  Post ReplyReply Direct Link To This Post Posted: 07-Jul-2011 at 9:59am
Thank for help Robert.

I do not want to run the method LoginAsync again, I just wanted to load another XAP and stay connected, so I created the class Managers, for not creating new instance and using same instance aways.(if you tell me otherwise). it is possible to stay connected?

Now the issue of code. if I instantiate a new EntityManager I need to remove the code, CompositionHost.Add if I did not remove show the same error (3), if I remove the code I can login, but when I run a query to populate the datagrid shows this error :

Type 'IdeaBlade.EntityModel.EntityQueryProxy`1[[SilverlightProject.Customer, SilverlightProject.Processing, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' is not known to the serializer. The following probe assemblies were found during initialization: SilverlightProject. Make sure that the assemblies holding your entity model and other known types are in this list. Check that your CompositionHost.SearchPatterns are correct. Also see the debug log to check for assembly load errors.

after code downloadXAP just this code (obs: I REMOVE CODE (CompositionHost.Add))

EntitiesTest em = new Entities();

em.LoginAsync(); //success

loginCompleted then

em.Customer.ExcuteAsync(); <= here the error;


thanks

Molinari
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down