现在的位置: 首页 > 综合 > 正文

在WCF RIA SERVICE Class Library中使用身份验证

2012年07月16日 ⁄ 综合 ⁄ 共 5042字 ⁄ 字号 评论关闭

      Silverlight Business Application项目模板为我们提供了身份验证功能,可以方便调用WebContext.Current获取当前登录的用户信息。

      现在我们为了实现Silverlight和WCF RIA SERVICE服务分层,所以加入了WCF RIA SERVICE CLASS LIBRARY,去除了默认的RIA SERVICE LINK。如下图的解决方案。我们有VDP,VDP.Web项目,这两个项目是Siverlight项目和WEB HOST。VDP.Service是RIA SERVICE CLASS LIBRARY项目。

      当我们移除VDP和VDP.WEB之间的RIA SERVICE LINK的时候,编译后不会在Generated_Code生成WebContext类。在VDP.SERVICE.WEB中新增Authentication Domain  Service也没有在VDP.Service生成WebContext。通过下面的描述方法可以解决此类问题(如果你有更好的请提供你的方法)。
新增下图的一系列项目:
sln

删除VDP.Service和VDP.Service.Web项目中的Class1.cs。
编译整个解决方案,这样VDP项目会在Generated_Code生成VDP.Web.g.cs。以备后面需要使用。
将VDP.WEB项目中的Models,Resources,Services拖到VDP.Service.Web中。并将VDP.WEB对应的目录删除。
将VDP.WEB项目引用的所有程序集拷贝到VDP.Service.Web的References中。如图: 
CopyReference 

pastereferences
Copy References需要安装PowerCommands的VS扩展,可以从这里下载获取到。PowerCommands

将VDP项目中的Assets,Helpers,Models,Web目录拖到VDP.Service项目中,然后把VDP.Service项目里的下面的文件删除:
Assets/Styles.xaml
Helpers\DataBindingExtensions.cs
Helpers\DataFieldExtensions.cs
Helpers\NotOperatorValueConverter.cs
Helpers\StringFormatValueConverter.cs
Helpers\TargetNullValueConverter.cs

在VDP项目中删除下面的文件:
Assets\Resources\
E:\ASheng\Project\VDP\VDP\VDP\Helpers\ResourceWrapper.cs

Models目录
Web目录

修改VDP.Service/Resources的资源文件的命名控件属性为:VDP.Service.Resources 
setResourceNamespace
同样修改VDP.Service.Web/Resources中的资源文件的命名空间为:VDP.Service.Web.Resources 

 setResourceNamespace2_temp
编辑VDP.Service和VDP的项目文件
editprojectfile

复制下面VDP.csproj文件中的下面的XML :

<Compile Include="..\VDP.Web\Resources\RegistrationDataResources.Designer.cs">

  <Link>Web\Resources\RegistrationDataResources.Designer.cs</Link>

  <AutoGen>True</AutoGen>

  <DesignTime>True</DesignTime>

  <DependentUpon>RegistrationDataResources.resx</DependentUpon>

</Compile>

<Compile Include="..\VDP.Web\Resources\ValidationErrorResources.Designer.cs">

  <Link>Web\Resources\ValidationErrorResources.Designer.cs</Link>

  <AutoGen>True</AutoGen>

  <DesignTime>True</DesignTime>

  <DependentUpon>ValidationErrorResources.resx</DependentUpon>

</Compile>

将此段XML复制到VDP.Service.csproj中,复制到在节点ItemGroup(包含Compile)节点里。如下图:

editprojectcompile

并把这段XML中字符VDP.Web修改成VDP.Service.Web

<Compile Include="..\VDP.Service.Web\Resources\RegistrationDataResources.Designer.cs">

  <Link>Web\Resources\RegistrationDataResources.Designer.cs</Link>

  <AutoGen>True</AutoGen>

  <DesignTime>True</DesignTime>

  <DependentUpon>RegistrationDataResources.resx</DependentUpon>

</Compile>

<Compile Include="..\VDP.Service.Web\Resources\ValidationErrorResources.Designer.cs">

  <Link>Web\Resources\ValidationErrorResources.Designer.cs</Link>

  <AutoGen>True</AutoGen>

  <DesignTime>True</DesignTime>

  <DependentUpon>ValidationErrorResources.resx</DependentUpon>

</Compile>

在VDP.csproj项目中,复制图中选中的XML代码,这代码是嵌入资源的,复制到VDP.Service.csproj的ItemGroup节点(含EmbeddedResource子节点),如下图: 

editprojectembedded

并将代码

<EmbeddedResource Include="..\VDP.Web\Resources\RegistrationDataResources.resx">

  <Link>Web\Resources\RegistrationDataResources.resx</Link>

  <Generator>PublicResXFileCodeGenerator</Generator>

  <LastGenOutput>RegistrationDataResources.Designer.cs</LastGenOutput>

</EmbeddedResource>

<EmbeddedResource Include="..\VDP.Web\Resources\ValidationErrorResources.resx">

  <Link>Web\Resources\ValidationErrorResources.resx</Link>

  <Generator>PublicResXFileCodeGenerator</Generator>

  <LastGenOutput>ValidationErrorResources.Designer.cs</LastGenOutput>

</EmbeddedResource>

改成

<EmbeddedResource Include="..\VDP.Service.Web\Resources\RegistrationDataResources.resx">

  <Link>Web\Resources\RegistrationDataResources.resx</Link>

  <Generator>PublicResXFileCodeGenerator</Generator>

  <LastGenOutput>RegistrationDataResources.Designer.cs</LastGenOutput>

</EmbeddedResource>

<EmbeddedResource Include="..\VDP.Service.Web\Resources\ValidationErrorResources.resx">

  <Link>Web\Resources\ValidationErrorResources.resx</Link>

  <Generator>PublicResXFileCodeGenerator</Generator>

  <LastGenOutput>ValidationErrorResources.Designer.cs</LastGenOutput>

</EmbeddedResource>

保存并重新加载项目 
newsln

打开VDP项目中的Generated_Code/VDP.Web.g.cs,Generated_Code是隐藏的文件夹。

复制WebContext类(在前面以开始编译生成的)。代码如下:

namespace VDP

{

    using System;

    using System.Collections.Generic;

    using System.ComponentModel;

    using System.ComponentModel.DataAnnotations;

    using System.Linq;

    using System.ServiceModel.DomainServices;

    using System.ServiceModel.DomainServices.Client;

    using System.ServiceModel.DomainServices.Client.ApplicationServices;

    using VDP.Web;

    

    

    /// <summary>

    /// Context for the RIA application.

    /// </summary>

    /// <remarks>

    /// This context extends the base to make application services and types available

    /// for consumption from code and xaml.

    /// </remarks>

    public sealed partial class WebContext : WebContextBase

    {

        

        #region Extensibility Method Definitions

 

        /// <summary>

        /// This method is invoked from the constructor once initialization is complete and

        /// can be used for further object setup.

        /// </summary>

        partial void OnCreated();

 

        #endregion

        

        

        /// <summary>

        /// Initializes a new instance of the WebContext class.

        /// </summary>

        public WebContext()

        {

            this.OnCreated();

        }

        

        /// <summary>

        /// Gets the context that is registered as a lifetime object with the current application.

        /// </summary>

        /// <exception cref="InvalidOperationException"> is thrown if there is no current application,

        /// no contexts have been added, or more than one context has been added.

        /// </exception>

        /// <seealso cref="System.Windows.Application.ApplicationLifetimeObjects"/>

        public new static WebContext Current

        {

            get

            {

                return ((WebContext)(WebContextBase.Current));

抱歉!评论已关闭.