Silverlight Feeds - All your Silverlight feeds in one place.

Sponsors

Thursday, May 14, 2009

Login with e-mail address using .net RIA services

by StefanOlson via Stefan Olson's Blog on 5/14/2009 9:16:43 AM

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

email it!bookmark it!digg it!

Original Post: Login with e-mail address using .net RIA services

Subscribe

New Feed

Product Spotlight

Recently Updated Sources

Legal Note

The content of the postings is owned by the respective author. Silverlight Feeds is not responsible for the contents of the postings. This site is automatically generated and cannot be reviewed for abusive content. If you find abusive content on Silverlight Feeds, please contact us. Designated trademarks and brands are the property of their respective owners. All rights reserved.

Advertise with us