locked
How to operate Cookie in Silverlight? RRS feed

  • Question

  • Hi all,

    I want to set cookie in Silverlight. But as far as I know, Silverlight is a plug-in and cookie is work for web. What should I do?

    Thanks.

    Tuesday, June 16, 2015 8:41 AM

Answers

  • Hi lcey,

    Following sample code explain the basic operation of write, read and delete. Please try with it, hope it helps.

    MainPage.xaml:

    <Grid x:Name="LayoutRoot" Background="LightBlue">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="97*"/>
                <ColumnDefinition Width="303*"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="23*"/>
                <RowDefinition Height="35*"/>
                <RowDefinition Height="42*"/>
            </Grid.RowDefinitions>
            <TextBlock HorizontalAlignment="Center" TextWrapping="Wrap" Text="Key:" VerticalAlignment="Center"/>
            <TextBlock HorizontalAlignment="Center" Grid.Row="1" TextWrapping="Wrap" Text="Value:" VerticalAlignment="Center"/>
            <TextBox Name="txtKey" Grid.Column="1" HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" Text="" VerticalAlignment="Center" Width="120"/>
            <TextBox Name="txtValue" Grid.Column="1" HorizontalAlignment="Left" Height="23" Grid.Row="1" TextWrapping="Wrap" Text="" VerticalAlignment="Center" Width="120"/>
            <Button Name="btnWrite" Content="Write" HorizontalAlignment="Left" Margin="26,56,0,0" Grid.Row="2" VerticalAlignment="Top" Width="75" Grid.ColumnSpan="2" Click="btnWrite_Click"/>
            <Button Name="btnRead" Content="Read" Grid.Column="1" HorizontalAlignment="Left" Margin="37,56,0,0" Grid.Row="2" VerticalAlignment="Top" Width="75" Click="btnRead_Click"/>
            <Button Name="btnDelete" Content="Delete" Grid.Column="1" HorizontalAlignment="Left" Margin="146,56,0,0" Grid.Row="2" VerticalAlignment="Top" Width="75" Click="btnDelete_Click"/>
    </Grid>
    

    MainPage.xaml.cs:

     private void btnWrite_Click(object sender, RoutedEventArgs e)
            {
                DateTime expir = DateTime.UtcNow + TimeSpan.FromDays(7);
                String cookie = String.Format("{0}={1};expires={2}", this.txtKey.Text, this.txtValue.Text, expir.ToString("R"));
                HtmlPage.Document.SetProperty("cookie", cookie);
    
            }
    
            private void btnRead_Click(object sender, RoutedEventArgs e)
            {
                String[] cookies = HtmlPage.Document.Cookies.Split(';');
                foreach (String cookie in cookies)
                {
                    String[] keyValues = cookie.Split('=');
                    if (keyValues.Length == 2)
                    {
                        if (keyValues[0].Trim() == this.txtKey.Text.Trim())
                        {
                            this.txtValue.Text = keyValues[1];
                        }
                    }
                }
    
            }
    
            private void btnDelete_Click(object sender, RoutedEventArgs e)
            {
                DateTime expir = DateTime.UtcNow - TimeSpan.FromDays(1);
                string cookie = String.Format("{0}=;expires={1}",this.txtKey.Text, expir.ToString("R"));
                HtmlPage.Document.SetProperty("cookie", cookie);
            }
    

    Best Regards,
    Weiwei

    • Marked as answer by Lcey Wednesday, June 17, 2015 8:28 AM
    Wednesday, June 17, 2015 2:25 AM
    Moderator

All replies

  • You can read and write cookies from Silverlight, no problem.

    HtmlPage.Document.SetProperty("MyCookie", value);

    Rather more than you probably wanted to know is in the following links:

    https://msdn.microsoft.com/en-gb/library/dd920298%28v=vs.95%29.aspx?f=255&MSPPError=-2147217396

    http://blogs.msdn.com/b/nikola/archive/2008/04/14/setting-cookies-through-silverlight.aspx


    Hope that helps.

    Technet articles: WPF: Change Tracking; All my Technet Articles

    Tuesday, June 16, 2015 1:39 PM
  • Hi lcey,

    Following sample code explain the basic operation of write, read and delete. Please try with it, hope it helps.

    MainPage.xaml:

    <Grid x:Name="LayoutRoot" Background="LightBlue">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="97*"/>
                <ColumnDefinition Width="303*"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="23*"/>
                <RowDefinition Height="35*"/>
                <RowDefinition Height="42*"/>
            </Grid.RowDefinitions>
            <TextBlock HorizontalAlignment="Center" TextWrapping="Wrap" Text="Key:" VerticalAlignment="Center"/>
            <TextBlock HorizontalAlignment="Center" Grid.Row="1" TextWrapping="Wrap" Text="Value:" VerticalAlignment="Center"/>
            <TextBox Name="txtKey" Grid.Column="1" HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" Text="" VerticalAlignment="Center" Width="120"/>
            <TextBox Name="txtValue" Grid.Column="1" HorizontalAlignment="Left" Height="23" Grid.Row="1" TextWrapping="Wrap" Text="" VerticalAlignment="Center" Width="120"/>
            <Button Name="btnWrite" Content="Write" HorizontalAlignment="Left" Margin="26,56,0,0" Grid.Row="2" VerticalAlignment="Top" Width="75" Grid.ColumnSpan="2" Click="btnWrite_Click"/>
            <Button Name="btnRead" Content="Read" Grid.Column="1" HorizontalAlignment="Left" Margin="37,56,0,0" Grid.Row="2" VerticalAlignment="Top" Width="75" Click="btnRead_Click"/>
            <Button Name="btnDelete" Content="Delete" Grid.Column="1" HorizontalAlignment="Left" Margin="146,56,0,0" Grid.Row="2" VerticalAlignment="Top" Width="75" Click="btnDelete_Click"/>
    </Grid>
    

    MainPage.xaml.cs:

     private void btnWrite_Click(object sender, RoutedEventArgs e)
            {
                DateTime expir = DateTime.UtcNow + TimeSpan.FromDays(7);
                String cookie = String.Format("{0}={1};expires={2}", this.txtKey.Text, this.txtValue.Text, expir.ToString("R"));
                HtmlPage.Document.SetProperty("cookie", cookie);
    
            }
    
            private void btnRead_Click(object sender, RoutedEventArgs e)
            {
                String[] cookies = HtmlPage.Document.Cookies.Split(';');
                foreach (String cookie in cookies)
                {
                    String[] keyValues = cookie.Split('=');
                    if (keyValues.Length == 2)
                    {
                        if (keyValues[0].Trim() == this.txtKey.Text.Trim())
                        {
                            this.txtValue.Text = keyValues[1];
                        }
                    }
                }
    
            }
    
            private void btnDelete_Click(object sender, RoutedEventArgs e)
            {
                DateTime expir = DateTime.UtcNow - TimeSpan.FromDays(1);
                string cookie = String.Format("{0}=;expires={1}",this.txtKey.Text, expir.ToString("R"));
                HtmlPage.Document.SetProperty("cookie", cookie);
            }
    

    Best Regards,
    Weiwei

    • Marked as answer by Lcey Wednesday, June 17, 2015 8:28 AM
    Wednesday, June 17, 2015 2:25 AM
    Moderator