Silverlight Roles, Profile and Authentication Example Updated for Silverlight Beta2 Now with Visual State Manager (VSM) goodness!


I am very excited about Silverlight Beta2 shipping recently…  I took a few minutes to update my recent sample "Accessing the ASP.NET Authentication, Profile and Role Service in Silverlight" post to Beta2.  While I was at it, I thought I’d take advantage of the new Visual State Manager (VSM) feature to simplify some of the UI. 

VSM is a new Beta2 feature of Silverlight (and soon WPF) that makes it much easier to define and transition between UI states in your application.  For more information  check out Christian Schormann’s VSM blog post and this great VSM screen cast and associated demo files which I stole from… ah, I mean "leveraged" in this example. 

Download:  Silverlight Beta2 Roles, Profile and Authentication Example Code (you need all the good stuff from http://silverlight.net/GetStarted/ to play)

 

Here are a few "states" now in the application.

Not logged in:

image

Transitioning to logged in…

image

Logged in as manager:

image

It is very simple to do this in blend… You simply use the new "States" tab in Blend to define a base state (highlighted below)  and a set of transition states…

image

For example, here is the logged in state…  the pattern I am using is basically to move Xaml elements on and off the screen

image

As you can see here, in the logged in state, the controls for logging in are simply moved off the screen.

image

 

Then the source code is super simple.  The key line is the first one in each function.  This where I programmatically tell Silverlight to change what state the UI should be in.   Much better than hiding a show a bunch of controls and very designer friendly.  Now I do not need to touch my code when some aspect of the design changes. 

private void LogIn_Button_Click(object sender, RoutedEventArgs e)
{
    VisualStateManager.GoToState(this, "LoggedIn", true);
    AuthenticationServiceClient client = new AuthenticationServiceClient();
    client.LoginCompleted += new EventHandler<LoginCompletedEventArgs>(client_LoginCompleted);
    client.LoginAsync(UserName.Text, Password.Password, "", true, UserName.Text);
}

private void LogOut_Button_Click(object sender, RoutedEventArgs e)
{
    VisualStateManager.GoToState(this, "LoggedOut", true);
    WelcomeMessage.Text = "logging out..";
    AuthenticationServiceClient client = new AuthenticationServiceClient();
    client.LogoutCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(client_LogoutCompleted);
    client.LogoutAsync();
}

 

Download:  Silverlight Beta2 Roles, Profile and Authentication Example Code (you need all the good stuff from http://silverlight.net/GetStarted/ to play).   Thanks to Hanu Kommalapati for help in porting to Silverlight beta2. 

I’d love to hear what you think!

 

Oh, while we are at it, Kathy updated her very popular controls sample page for Silverlight Beta2 as well. 

clip_image001

Silverlight Control Demo Sample

A sample of twenty-four Silverlight 2 controls that can be viewed live together with the source code used to drive the controls.

By clicking Download It you accept the license

View ItDownload It

Comments (17)

  1. gootenks says:

    The quality of MS dev products goes down with each release :(. I’ve installed Silverlight Beta2 and now VS2008 Professional crashes *every time* I open *any* Silverlight project.

    On another computer I’ve installed VS2008 SP1 and now I *cannot* open *any* Silverlight project – I get "Project is not supported by this version of VS …".

    What’s going on with y ou guys ?

  2. Pete Brown says:

    @gootenks

    Keep in mind this is a beta, not a real "release" so I don’t think your comment is entirely fair.

    That said, hop on over to Silverlight.net and post your question in the getting started forum there. There are tons of people available and willing to help you out. We’re all excited about Silverlight and all in it together.

    Pete

  3. gootenks says:

    > Keep in mind this is a beta, not a real "release" so

    > I don’t think your comment is entirely fair.

    Yes, I know, and maybe I have "overreacted". I am

    sorry and I appologize.

    But *the fact* is that the current release is broken.

    I was really excited seeing this release but now I am

    really disapointed.

    To install Silverlight Tools for VS2008 I had to uninstall

    ENU (KB949325), otherwise I got  0x80070643 error.

    Fortunately guys commenting on Scott’s blog were

    smart enough to discover this.

    But it could be a minor problem if VS would not

    crash *every* time I open and close Silverlight

    project. Now it is a real showstopper. I cannot

    proceed with my development. And I don’t know

    if I can revert to Beta1 to be able to continue my

    work.

    Beta means no more and no less than beta. But

    I would at least expect that Beta2 is better than Beta1.

  4. Pete Brown says:

    @gootenks

    Please post over on Silverlight.net. Lots of folks have the beta running on their machines (me included) without issues. Lots of other folks have run into various issues which we’ve helped address.

    Pete

  5. BradleyB on SL2B2 Installation, WPFDevCon on SL2B2 Docs, Eric Hexter on Testing SL2B2, Brad Abrams on

  6. Ozzy says:

    The authentication example you posted is really cool and works like a charm on the client side, but what about the server side?  If I make a call to a web service on the same server with which I authenticated, how can I tell from within that service which user is making the call?

  7. BradA says:

    >>The authentication example you posted is really cool and works like a charm on the client side, but what about the server side?  If I make a call to a web service on the same server with which I authenticated, how can I tell from within that service which user is making the call?

    Ozzy – THe great thing about this support is that it leverages the existing ASP.NET provider model that is very mature.  http://wiki.asp.net/page.aspx/275/provider-model

  8. BradA says:

    >>The authentication example you posted is really cool and works like a charm on the client side, but what about the server side?  If I make a call to a web service on the same server with which I authenticated, how can I tell from within that service which user is making the call?

    Ozzy – THe great thing about this support is that it leverages the existing ASP.NET provider model that is very mature.  http://wiki.asp.net/page.aspx/275/provider-model

  9. Rui Marinho says:

    i can’t get the source, is it the link online? and working?

  10. Ozzy says:

    >> Ozzy – THe great thing about this support is that it leverages the existing ASP.NET provider model that is very mature.  

    Yes I understand, but here’s the problem I am having: If you authenticate using your method and call the web service (an ASP.NET proxy service, not WCF), the current user reported by the Membership API is null. Also, since the cookie is not passed on the call, there is no HTTP context from which to get the authenticated user.  However, if you redirect to an login.aspx page for doing a normal ASP.NET login, you can get the current user by either of those two methods.

    I have seen an example where there is a way to pass the cookie using ADO.NET Data Services, but that will not work in SL since the required interfaces are not exposed.

    Is there a way to do this in SL?

  11. Rui Marinho says:

    Hi , i still can get this beta2 code for the sample, can u fix it brad?

    i don’t know if it helps, but in my app in beta1 i use de default asp login and then i pass if the user is authenicated, username and password, by parameter of the silverlight app, then in silverlight app.cs i do

    string utilizador = e.InitParams["user"]

    then i use that and the service of profile and membership in the silverlight app with no problem.

  12. Percorso formativo in italiano Aggiornamento del percorso formativo su Silverlight 2, ecco i link diretti,

  13. Percorso formativo in italiano Aggiornamento del percorso formativo su Silverlight 2, ecco i link diretti,

  14. Ian Sorb says:

    Interesting technique by translating controls on and off the screen based on a state transition.

    But just because a control is no longer in visual range, surely it could still be accessed via keyboard shortcuts and tabbing etc…

    In the case of some controls being made "non-visual" based on not being logged in, or not having the right permission – this could be a security problem. I would also disable the control as part of the state change…

    Ian.

  15. cnblogs.com says:

    ASPX: 1.在WEB项目中同时使用 C# AND VB 开发: 2. 微软SQL注入分析工具源代码下载。 3. 某些网站可能无法在 Internet Explorer 8 Beta 1 中正确显示的解决方案

  16. Updated 18/7/2008 Percorso formativo in italiano Aggiornamento del percorso formativo su Silverlight