【问题标题】:Sharing authentication with ASP.NET and Silverlight与 ASP.NET 和 Silverlight 共享身份验证
【发布时间】:2011-08-09 14:40:26
【问题描述】:

我有一个包含一些 silverlight 页面的 ASP.NET Web 应用程序。首先,用户使用通用的 ASP.NET 网络表单进行身份验证。

这将是典型的代码:

FormsAuthentication.SetAuthCookie(this.txtUsername.Text, false);
FormsAuthentication.RedirectFromLoginPage(this.txtUsername.Text, false);
Response.Redirect("~/Private/Index.aspx");

接下来,用户导航到包含 Silverlight 页面的页面。从那个 silverlight 页面,我需要安全地调用一些 WCF 服务。

服务包含以下代码:

[ServiceContract(Namespace = "http://mydomain")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
    public class ServiceWCF
    {
        public ServiceWCF()
        {
            System.ServiceModel.Web.WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.OK;

            Thread.CurrentPrincipal = HttpContext.Current.User;
        }

        [OperationContract]
        [PrincipalPermission(SecurityAction.Demand, Authenticated = true)]
        public List<Data> GetData()
        {
            // do things
        }
    }

这就是我从 silverlight 客户端调用该方法的方式:

MyWCFReference.ServiceWCFClient proxy = new MyWCFReference.ServiceWCFClient();

proxy.GetDataCompleted += new EventHandler<MyWCFReference.GetDataCompletedEventArgs>(proxy_GetDataCompleted);
proxy.GetDataAsync();

我按照这篇文章来保护我的网络服务: http://netpl.blogspot.com/2010_04_01_archive.html

但是当我运行我的代码时,我得到了错误:Request for principal permission failed.

知道如何解决它吗?啊啊啊!!

【问题讨论】:

    标签: asp.net silverlight wcf security


    【解决方案1】:

    您可能需要在托管 WCF 服务的域的根目录中设置 clientaccesspolicy.xmlcrossdomain.xml 文件,以允许您的 Silverlight 应用程序访问。

    查看MSDN page了解完整详情。

    【讨论】:

    • @xus - 哦,好吧。添加尝试建立连接的代码可能是个好主意。
    • 问题是 WCF 服务在不同的项目中(在同一个解决方案中)。我将 WCF 服务移至 asp.net 项目,现在它可以工作了:)
    猜你喜欢
    • 2011-04-22
    • 1970-01-01
    • 1970-01-01
    • 2011-11-23
    • 2018-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多