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 を作成し、ユーザーを Web サイトのルート ページにリダイレクトしました。 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 Web ページでユーザーを認証した後、Silverlight アプリのスタートアップ コードで新しいインスタンスを作成する代わりに、認証済みの WebContext インスタンスをログオン ページから Silverlight アプリケーションに渡す必要があるかどうか疑問に思っていますが、それを行う方法がわかりません。

任意またはすべての提案に感謝します。

4

1 に答える 1

0

私はResponse.Redirect("...", true);

この記事によると、セッションを維持するには false を渡す必要があります

于 2012-01-11T06:47:05.910 に答える