0

这应该只是工作。我已经阅读了我可以通过 google 找到的关于该主题的所有文章,并尝试从 StackOverflow 和 CodeProject 等其他文章中尽可能多地复制,但无论我尝试什么 - 它都不起作用。

我有一个使用 Windows 身份验证运行良好的 silverlight 应用程序。

为了让它在表单身份验证下运行,我已经:

编辑 web.config 文件以启用表单身份验证(并删除 Windows 身份验证配置):

    <authentication mode="Forms">
    <forms name=".ASPXAUTH" loginUrl="logon.aspx" defaultUrl="index.aspx"      protection="All" path="/" timeout="30" />
    </authentication>

在页面后面创建了一个标准的logon.aspx和logon.aspx.cs代码来获取用户输入的用户名和密码,并在登录成功时创建一个身份验证cookie,然后将用户重定向到网站的根页面,这是一个silverlight应用程序:

    private void cmdLogin_ServerClick( object sender, System.EventArgs e )
    {
        if ( ValidateUser( txtUserName.Value, txtUserPass.Value ) )
        {
            FormsAuthentication.SetAuthCookie(txtUserName.Value, true);
            var cookie = FormsAuthentication.GetAuthCookie(txtUserName.Value, true);
            cookie.Domain = "mymachine.mydomain.com";
            this.Response.AppendCookie(cookie);

            string strRedirect;
            strRedirect = Request["ReturnUrl"];
            if ( strRedirect == null )
                strRedirect = "index.aspx";
            Response.Redirect( strRedirect, true );
        }
    }

因此,成功登录后的重定向会启动我的 silverlight 应用程序。

但是,在执行 Silverlight 启动代码时,用户未经过身份验证:

    public App()
    {
        InitializeComponent();
        var webContext = new WebContext();
        webContext.Authentication = new FormsAuthentication();
        ApplicationLifetimeObjects.Add( webContext );
    }

    private void ApplicationStartup( object sender, StartupEventArgs e )
    {
        Resources.Add( "WebContext", WebContext.Current );

        // This will automatically authenticate a user when using windows authentication
        // or when the user chose "Keep me signed in" on a previous login attempt
        WebContext.Current.Authentication.LoadUser(ApplicationUserLoaded, null);

        // Show some UI to the user while LoadUser is in progress
        InitializeRootVisual();
    }

错误发生在 ApplicationUserLoaded 方法中,该方法在进入该方法时始终将其 HasError 属性设置为 true。

    private void ApplicationUserLoaded( LoadUserOperation operation )
    {
        if((operation != null) && operation.HasError)
        {
            operation.MarkErrorAsHandled();
            HandlerShowWebServiceCallBackError(operation.Error, "Error loading user context.");
            return;
        }
        ...
    }

报告的错误如下 - 在我看来,用户在进入 silverlight 应用程序时未被视为经过身份验证,因此它指示代码尝试返回登录页面,该页面返回的数据意外银光应用:

An exception occurred while attempting to contact the web service.
Please try again, and if the error persists, contact your administrator.

Error details:
Error loading user context.

Exception details:
Load operation failed for query 'GetUser'. The remote server returned an error: NotFound.

有任何想法吗?

根据我阅读的所有内容,这应该非常简单并且可以正常工作 - 所以我显然犯了一个非常基本的错误。

我想知道在我的 logon.aspx 网页上对用户进行身份验证后,我是否需要以某种方式将经过身份验证的 WebContext 实例从登录页面传递到我的 silverlight 应用程序,而不是在 silverlight 应用程序启动代码中创建一个新实例 - 但是不知道该怎么做。

欣赏任何或所有建议。

4

1 回答 1

0

我怀疑Response.Redirect("...", true);

根据这篇文章,你应该通过 false 来保持会话

于 2012-01-11T06:47:05.910 回答