views:

1230

answers:

1

I am creating a silver light application using Navigation app template. It is for internal use and hence uses windows authenticatoin. There is a dashboard page which shows couple of records filtered by logged in users id. To get the user id (which is an int) I call a web service by overriding the GetAuthenticatedUser and pass the username (from IPrincipal). This service takes some time to return the details.

When I navigate to dashboard app, it renders completely with no data because the user service is a async operation and I am not able to make the rendering wait till my GetAuthenticatedUser finishes completely. So I created a Login page which just shows a progress bar till I get the user object and then navigate to dashboard. If someone tries to access the dashboard directly by using the URL, i want them to navigate back to Login page.

So in the dashboard constructor I added the following code

        if (!UserService.Current.User.IsAuthenticated)
        {
            MessageBox.Show("Navigating away");
            Frame objContainer = this.Parent as Frame;
            objContainer.Navigate(new Uri("/Views/Login.xaml", UriKind.Relative));
        }

Thogh I get the message box prompt, it does not actually take me to Login page but stays in dashboard page. I also tried putting this code in OnNavigatedTo override with no luck.

I also tried using NavigationService instead of Frame as below, with no luck

        if (!UserService.Current.User.IsAuthenticated)
        {
            MessageBox.Show("Navigating away");
            this.NavigationService.Navigate(new Uri("/Views/Login.xaml", UriKind.Relative));
        }

it still does not work. Does anyone know how to make some page accessible only if I have fully valid user object? if they try to access the restricted page without this, I want them to be able to redirected to Login page, how can this be achieved?

I am using Silverlight 3 Beta

Shreedhar

A: 

I finally found a way around this. In the Constructo i Hooked up the Loaded event handler and in the event handler I am navigating to a different page and it works fine now.

    public Dashboard()
    {
        InitializeComponent();
        this.Loaded += new RoutedEventHandler(Dashboard_Loaded);
    }

    void Dashboard_Loaded(object sender, RoutedEventArgs e)
    {
        if (!UserService.Current.User.IsAuthenticated)
        {
            Frame objContainer = this.Parent as Frame;
            if (objContainer != null)
            {
                objContainer.Navigate(new Uri("/Views/Login.xaml", UriKind.Relative));
            }
        }
    }

This piece of code works just fine!

Shreedhar

Shreedhar