【Silverlight】限制Silverlight应用只能在指定的域名下使用,保护你的SL程序

介于国许多行业的某些行为,有必要限制Silverlight应用只能在指定的域名下使用。为了达到这一目的,特写一样例,仅供参考。

这是在当前页面正常执行的结果:

这是同一个程序不在指定域名下运行的结果(截图):

实现方法:

1、添加AccessDeniedPage.xaml,增加一个构造函数

        public AccessDeniedPage(Uri uri)
            : this()
        {
            this.DataContext = uri;
        }

2、设置AccessDeniedPage.xaml前台:

<UserControl x:Class="CheckAccess.AccessDeniedPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">
    
    <Grid>
    
		<Grid Background="White" VerticalAlignment="Center" Height="300">
			<TextBlock Text="请从原始页面访问" HorizontalAlignment="Center" VerticalAlignment="Top" FontSize="29.333" Margin="0,75,0,0"/>
			<HyperlinkButton Content="{Binding}" Margin="0,0,0,100" HorizontalAlignment="Center" VerticalAlignment="Bottom" NavigateUri="{Binding}" FontSize="21.333"/>
		</Grid>
	</Grid>
</UserControl>

2、给App类加上uri字段,把Application_Startup修改为:

        private void Application_Startup(object sender, StartupEventArgs e)
        {
            bool flag = false;
            try
            {
                flag = HtmlPage.Document.DocumentUri.Host.ToLower() == uri.Host.ToLower();
            }
            catch { }
            if (flag)
                this.RootVisual = new MainPage();
            else
                this.RootVisual = new AccessDeniedPage(uri);
        }

3、如果你自己部署的xap和html不在同一个域名下,还需要设置

<param name="enableHtmlAccess" value="true" />
原因参考:http://msdn.microsoft.com/zh-cn/ff686925.aspx

下载 SLCheckAccess.zip

原文地址:https://www.cnblogs.com/Aimeast/p/2012269.html