This project has moved. For the latest updates, please go here.

Helpers for Login/Logout

Getting login/logout right in an application can be tricky. AgFx provides some helpers here.

This stuff is in AgFx.Controls.Authentication in the AgFx.Controls assembly.

PhoneApplicationFrameEx for showing Login UI

PhoneApplicationFrameEx provides a "PopupContent" property which, which the "IsPopupVisible" property is set to true, will obscure the other UI for the app. So this is a handy place to stick some login UI.

In your app.xaml.cs, replace all references to PhoneApplicationFrame with PhoneApplicationFrameEx.

Then, add this code:

RootFrame.PopupContent = new LoginControl();          
Binding b = new Binding("IsLoggedIn");
b.Source = MyLoginModel.Current;
b.Converter = new NotConverter();
RootFrame.SetBinding(PhoneApplicationFrameEx.IsPopupVisibleProperty, b);

What this says is "Bind the NOT of the current login state to the IsPopupVisible property of the frame".  In other words, when the user is logged in, hide the login UI.  And vice versa.

 

LoginLoadContext lets you specify credentials

Next, we'll need a way to specify credentials.  LoginLoadContext has, by default a Login and a Password property.  Whatever your site/service uses for a login id (email, username, etc), use that here.  You can derive from LoginLoadContext if you need to specify other information, or if you want to call it something else.  

It also has a "CanAttemptLogin" property that can be overriden.  By default, it returns false if Password is null.  This prevents a load request from happening.

LoginModel holds and caches the token

Now we get to the good stuff.  For all of this to work, we need to create our own LoginModel class.  Typically you'll want to put things on here like the current user ID or name.  

You also need to specify a static "Current" property, this is important.

Something like this:

 

public class MyLoginModel : LoginModel {

        public static MyLoginModel Current {
            get {
                return GetCurrentLoginModel<MyLoginModel, LoginLoadContext>();
            }
        }

        private string _userId;
        public string UserId {
            get {
                return _userId;
            }
            set {
                if (_userId != value) {
                    _userId = value;
                    RaisePropertyChanged("UserId");
                }
            }
        }
}

 

 

LoginDataLoader fetches the response

Now, like with all models we need to create our loader.  This one only will load if the LoginLoadContext.CanAttemptLogin returns true.

The code is also straight forward, like this:

 public class MyLoginModel : LoginModel {

       ///  snip...
       
        public class MyLoginLoader : LoginDataLoader<LoginLoadContext> {

            // This will only be called if my LoginLoadContext.CanAttemptLogin property is true.
            protected override AgFx.LoadRequest BuildLoginRequest(LoginLoadContext context) {
                Uri uri = new Uri(String.Format("https://www.myservice.com/login?username={0}&password={1}", context.Login, context.Password));
                return new WebLoadRequest(context, uri);
            }

            public override object Deserialize(LoginLoadContext loadContext, Type objectType, System.IO.Stream stream) {

                var loginMessage = MyServiceHelpers.ParseLoginResponse(stream);

                MyLoginModel model = new MyLoginModel();
                model.LoadContext = loadContext;
                model.Token = loginMessage.Token;
                model.UserId = loginMessage.UserId;
                return model;
            }
        }

    }

Using info about the login in your application

Now, back to why we have Current.  It's because we now have a very handy way to get the info we need to make other calls with MyLoginModel.Current.Token and MyLoginModel.Current.UserId.

Last edited Apr 2, 2011 at 1:02 AM by shawnburke, version 1

Comments

No comments yet.