Login with e-mail address using .net RIA services

by StefanOlson 14. May 2009 09:16

I'm currently working a very interesting project using Silverlight 3 and the new .net RIA services. Basically we're building the whole e-commerce type website using Silverlight 3. If you don't have Silverlight 3 installed you'll be presented with HTML versions of the pages that you can browse and if you need to login or use any of the interactive features then we will require you to install Silverlight 3.

I still have concerns about the install base of Silverlight. The site is entirely focused on New Zealand users According to riastats.com, 62.5% of New Zealanders do not have Silverlight installed. This differs from the statistics provided by some developers who work for ACP media, where they said around 80% don't have Silverlight installed (ACP Media Development Leads To Pure Direction).  Either way, there is still a lot of people who do not have Silverlight installed, although it is reducing over time. This won't be a long-term problem, but is a major challenge for Microsoft when it comes to websites adopting Silverlight.

The website makes extensive use of the new .net RIA services, which is currently in preview. If you haven't heard about this, it provides a way to access your database information from a Silverlight application. For a good overview see Brad Abrams blog, here.

For a preview release, the .net RIA services seem incredibly stable. I haven't encountered any serious bugs, at this stage.

Because many users quickly forget the username for a specific site I want people to be able to login using their e-mail address. I recently started a thread on the Silverlight.net RIA services forum to try and find out if this was possible.  What I wanted to be able to do was to override the login function on the authentication service and replace the given username, which was an e-mail address with the user's actual username. The code would've looked something like this:

public class AuthenticationWithEmailLogin<T> : AuthenticationBase<T> where T : UserBase, new()
{
    public override User Login(string userName, string password, bool isPersistent)
    {
        string newUserName = Membership.GetUserNameByEmail(userName);
        if (newUserName != null) // if the user has provided an e-mail address use the username associated with that
        {
            userName = newUserName;
        }
        return base.Login(userName, password, isPersistent);
    }
} 

Unfortunately, Login is not virtual, so I can't use that option.

Eventually, Kyle suggested that I override several other functions in order to make it do what I want.  Here's what I ended up with ( simplified after some further suggestions from Kyle):

public class AuthenticationWithEmailLogin<T> : AuthenticationBase<T> where T : UserBase, new()
{
    protected virtual string GetUserNameToUse(string userName)
    {
        string newUserName = Membership.GetUserNameByEmail(userName);
        if (newUserName != null)
        {
            userName = newUserName;
        }
        return userName;
    }

    protected virtual IPrincipal GetPrincipalWithCorrectName(IPrincipal principal)
    {
        return new GenericPrincipal(
                new GenericIdentity(GetUserNameToUse(principal.Identity.Name), principal.Identity.AuthenticationType),
                new string[0]);
    }

    protected override bool ValidateUser(string userName, string password)
    {
        return base.ValidateUser(GetUserNameToUse(userName), password);
    }

    protected override T GetAuthenticatedUser(IPrincipal principal)
    {
        return base.GetAuthenticatedUser(GetPrincipalWithCorrectName(principal));
    }

    protected override void IssueAuthenticationToken(IPrincipal principal, bool isPersistent)
    {
        base.IssueAuthenticationToken(GetPrincipalWithCorrectName(principal), isPersistent);
    }
}

You can download my complete class from here

Hopefully it’ll help someone.

…Stefan

Tags:

Silverlight | .net ria services

Comments

6/1/2009 9:54:17 PM #

I am also working on an SL3 project using .NET RIA Services and have a similar requirement. Can you please let me know the client side story of using this AuthenticationWithEmailLogin?

Sameer

6/2/2009 5:00:00 PM #

Sameer ,

sorry for the delay in replying, unfortunately almost all messages notifying me of comments on my blog never actually arrive, whether this is the fault of the blog software or some problem with the e-mail I'm not sure.

In any case on the client side all you need to do is use the existing login function but pass an e-mail address.  The server-side will then do all the work. e.g:

UserService _userService = UserService.Current;
_userService.LoginCompleted += userService_LoginCompleted;
_userService.Login(userName, password);

...Stefan

StefanOlson

Add comment




biuquote
  • Comment
  • Preview
Loading



About the author

Stefan Olson is the Managing Director of Olson Software.  He has been developing software using Microsoft Technologies for nearly 20 years.

He is currently working on building the next generation Virtual Tour software in WPF and Silverlight for www.palacevirtualtours.com.

Tag cloud