New Posts New Posts RSS Feed: Accessing Principal from VM
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Accessing Principal from VM

 Post Reply Post Reply
Author
gregweb View Drop Down
DevForce MVP
DevForce MVP
Avatar

Joined: 10-Sep-2009
Location: Clearwater, Fl
Posts: 253
Post Options Post Options   Quote gregweb Quote  Post ReplyReply Direct Link To This Post Topic: Accessing Principal from VM
    Posted: 04-Jul-2013 at 2:39pm
I am populating Principal.Roles on the server.

However, it is not clear how to access the Principal on the client. Since Cocktail doesn't allow direct access to the EntityManager, I can't use EntityManager.Principal directly.

The only way I have found so far is to import the AuthenticationService into the viewModel, and then check AuthenticationService.Principal.

Am I doing this correctly?

Greg
Back to Top
mgood View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 18-Nov-2010
Location: Emeryville, CA
Posts: 583
Post Options Post Options   Quote mgood Quote  Post ReplyReply Direct Link To This Post Posted: 05-Jul-2013 at 9:05pm
Yes, that is the correct way. The principal should always be retrieved from the AuthenticationService. Instead of injecting the AuthenticationService, though, a cleaner approach is to create a UserService from which you can obtain the principal and other authorization stuff. The UserService uses the AuthenticationService to get the principal and for example cast it to the proper type if you use a custom principal. You can see it in TempHire, although TempHire doesn't actually use the UserService anywhere yet, but it shows the idea. The idea of this approach is to cleanly separate authentication from authorization.

The interface looks like this:

    public interface IUserService
    {
        bool IsLoggedIn { get; }

        UserPrincipal CurrentUser { get; }
    }

And the implementation like this. The custom AuthenticationService acts in both capacities, but from the consumption perspective, the two concerns are separated. 

    [Export(typeof(IAuthenticationService))]
    [Export(typeof(IUserService))]
    [PartCreationPolicy(CreationPolicy.Shared)]
    public class TempHireAuthenticationService : AuthenticationService, IUserService
    {
#if FAKESTORE
        public TempHireAuthenticationService()
        {
            Configure(config => config.WithConnectionOptions(ConnectionOptions.Fake.Name));
        }
#endif

        #region IUserService Members

        public UserPrincipal CurrentUser
        {
            get { return Principal as UserPrincipal; }
        }

        #endregion

        protected override void OnLoggedIn()
        {
            base.OnLoggedIn();
            EventFns.Publish(new LoggedInMessage(CurrentUser));
        }

        protected override void OnLoggedOut()
        {
            base.OnLoggedOut();
            EventFns.Publish(new LoggedOutMessage());
        }
    }

Back to Top
gregweb View Drop Down
DevForce MVP
DevForce MVP
Avatar

Joined: 10-Sep-2009
Location: Clearwater, Fl
Posts: 253
Post Options Post Options   Quote gregweb Quote  Post ReplyReply Direct Link To This Post Posted: 06-Jul-2013 at 6:21am
Great, thanks Marcel.
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down