1、准备工作 新建一个Silverlight Business
Application,
首先修改web.config,他自动生成的配置比较省略,我
手动加入membership,role,profile的配置,我是ASP.NET MVC的项目中拷贝过来,直接用他的配置也可以,
不过手动配置一下连接字符串LocalSqlServer,不然不能运行。 <membership>
<providers>
<clear />
<add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/Vega" />
</providers>
</membership>
<profile>
<providers>
<clear />
<add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/" />
</providers>
<properties>
<add name="FriendlyName" />
</properties>
</profile>
<roleManager enabled="true">
<providers>
<clear />
<add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="ApplicationServices" applicationName="/Vega" />
<add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/" />
</providers>
</roleManager>
我用的连接字符串名字是ApplicationServices,如果没安装ASP.NET的SQL数据,可以用Visual Studio 命令提示(2010) (在开始菜单的Visual Studio Tools里)调出命令提示符输入aspnet_regsql,调出“ASP.NET SQL SERVER安装向导”,而在运行-cmd里因为没有相关环境变量,是调不出安装向导的。
新建一个ADO.NET实体数据模型。
编译一下,然后新建Domain Service Class,勾上Course,如果不编译,那么数据实体模型是找不到的。我们需要Microsoft Silverlight 4 Toolkit,http://silverlight.codeplex.com/,没安装的话要先安装。
2、用户登录和角色 用户登录后
往往要根据
不用角色实现不同的功能导航,
所以要修改/Views/Login/LoginStatus.xaml 这个登录状态的用户控件。首先加上管理员的功能面板。 <StackPanel x:Name="adminControls" Style="{StaticResource LoginPanelStyle}">
<!-- welcomeText.Text property's binding is setup in code-behind -->
<TextBlock x:Name="welcomeAdminText" Style="{StaticResource WelcomeTextStyle}" VerticalAlignment="Center"/>
<!-- welcomeText.Text property's binding is setup in code-behind -->
<TextBlock Text=" | " Style="{StaticResource SpacerStyle}"/>
<HyperlinkButton TargetName="ContentFrame" Content="课程" VerticalAlignment="Center" Foreground="White" NavigateUri="/Courses"/>
<TextBlock Text=" | " Style="{StaticResource SpacerStyle}"/>
<HyperlinkButton TargetName="ContentFrame" Content="学员" VerticalAlignment="Center" Foreground="White" NavigateUri="/Students"/>
<TextBlock Text=" | " Style="{StaticResource SpacerStyle}"/>
<HyperlinkButton TargetName="ContentFrame" Content="排课" VerticalAlignment="Center" Foreground="White" NavigateUri="/Schedules"/>
<TextBlock Text=" | " Style="{StaticResource SpacerStyle}"/>
<Button x:Name="adminLogoutButton" Content="{Binding ApplicationStrings.LogOffButton, Source={StaticResource ResourceWrapper}}"
Click="LogoutButton_Click"
Style="{StaticResource LoginRegisterLinkStyle}"
IsEnabled="{Binding
Authentication.IsLoggingOut, Converter={StaticResource NotOperatorValueConverter}}" />
</StackPanel>
然后增加一个状态,叫做adminLoggedIn,在<vsm:VisualStateGroup x:Name="loginStates">代码短里加上adminLoggedIn的代码:
<vsm:VisualState x:Name="adminLoggedIn">
<Storyboard>
<ObjectAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="logoutControls" Storyboard.TargetProperty="(UIElement.Visibility)">
<DiscreteObjectKeyFrame KeyTime="00:00:00.0000000">
<DiscreteObjectKeyFrame.Value>
<Visibility>Collapsed</Visibility>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="loginControls" Storyboard.TargetProperty="(UIElement.Visibility)">
<DiscreteObjectKeyFrame KeyTime="00:00:00.0000000">
<DiscreteObjectKeyFrame.Value>
<Visibility>Collapsed</Visibility>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</