2012-01-10 5 views
0

これはちょうど動作するはずです。私はトピックでGoogleを介して見つけることができるすべての記事を読んだが、StackOverflowとCodeProjectと他の両方の他の記事からコピーできるようにしようとしたが、何を試みても問題ありません。Silverlight 4アプリケーションで使用されるASP.NETログオンページからフォーム認証を取得する

Windows認証を使用して正常に動作するSilverlightアプリケーションがあります。

それはフォーム認証の下で実行されている取得するために私がしました:フォーム認証を有効にする(とWindows認証の設定を削除)するためにweb.configファイルを編集

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

は、標準のログオンを作成しました。 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(); 
    } 

のエラーは常にHasErrorプロパティはメソッドへのエントリ上でtrueに設定されたApplicationUserLoaded方法で起こります。

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アプリケーションに渡す必要があるのでしょうかスタートアップコード - しかし、どのようにそれを行うか分かりません。

いずれかまたはすべての提案を気に入ってください。

+0

iircのデフォルトでは、Silverlightプロジェクトはフォーム認証として設定されています。既定のプロジェクトを作成して設定を比較しようとしましたか? –

+0

私は、あなたが 'FormsAuthentication'の新しいインスタンスを作成しようとしているのであれば、それがうまくいくと確信しています。 – Kassem

+0

提案していただきありがとうございます。私はSilverlightアプリケーションを最初から書いている経験はあまりありませんが、既存のものを修正するにはかなりの経験があります。私はちょうど既定のSilverlight Hello World型アプリケーションを作成しましたが、設定を比較することによって明らかな手がかりを得られませんでした:-( – Paul

答えて

0

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

According to this article you should pass false to keep the sessionを疑います。

+0

提案をいただきありがとうございますElmo - あなたは正しいです。しかし、残念ながらそれは私の問題を解決しなかった - 結果は同じだった。 – Paul

+0

SLアプリケーションを含むaspxページにLoginViewコントロールを追加すると、認証されたユーザーを示しますか? –

+0

はい。 aspxページは、認証されたユーザーを示します。 SLアプリケーションに入ると、認証されたユーザーのサインはありません。 – Paul

関連する問題