2013-05-16 11 views
7

何らかの理由で、私の認証CookieのUserDataプロパティが空です。ここでは、コードは次のようになります。ここでは設定されているにもかかわらず、FormsAuthenticationTicketのUserDataプロパティが空です

var authCookie = FormsAuthentication.GetAuthCookie(userName, rememberUser.Checked); 
// Get the FormsAuthenticationTicket out of the encrypted cookie 
var ticket = FormsAuthentication.Decrypt(authCookie.Value); 
// Create a new FormsAuthenticationTicket that includes our custom User Data 
var newTicket = new FormsAuthenticationTicket(ticket.Version, ticket.Name, ticket.IssueDate, ticket.Expiration, ticket.IsPersistent, "userData"); 
// Update the authCookie's Value to use the encrypted version of newTicket 
authCookie.Value = FormsAuthentication.Encrypt(newTicket); 
// Manually add the authCookie to the Cookies collection 
Response.Cookies.Add(authCookie); 
FormsAuthentication.RedirectFromLoginPage(userName, rememberUser.Checked); 

は、私はそれにアクセスしようとする方法である:

if (HttpContext.Current.Request.IsAuthenticated) 
{ 
    var authCookie = HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName]; 
    if (authCookie != null) 
    { 
     var authTicket = FormsAuthentication.Decrypt(authCookie.Value); 
     string data = authTicket.UserData; 
     // data is empty !!! 
    } 
} 

答えて

7

RedictFromLoginPageはあなたのクッキーが上書きされます。この行を削除し、手動でリダイレクトしてください(Response.Redirect)。

+0

で働く' UserData'が得た空の文字列 – Ravi

+0

@Ravi:質問としてあなたのコードスニペットを投稿してください。 –

+0

私は自分の質問を投稿しました[http://stackoverflow.com/questions/20816425/getting-formsauthentication-ticket-userdata-as-empty-string](http://stackoverflow.com/questions/20816425/getting-formsauthentication- ticket-userdata-as-empty-string) – Ravi

3

これは私が数日前に回答したのと同じ回答です。自分でFormsAuthenticationTicketを作成する場合は、FormsAuthentication.SetAuthCookieまたはFormsAuthentication.RedirectFromLoginPageを使用することはできません

https://stackoverflow.com/a/16365000/296861

+0

FormsAuthentication.GetRedirectUrlにもこの問題があります。 URLを取得して変数に格納し、次にCookieを設定し、最後にリダイレクトを実行して問題を解決しました。 –

0

それはあなたが同じチュートリアルをやっていた私には思える... iは

<authentication mode="Forms"> 
    <forms slidingExpiration="true" timeout="60" cookieless="UseUri"/> 
    </authentication> 

あなたが持っている場合はCookieなし= ..それがWeb構成エラーだった..私の場合には同じ問題 が発生しましたあなたのWeb設定で「UseUri」それを削除... を、それは私はまだ `Response.Redirect`しかしを使用しています私の場合

関連する問題