2012-04-17 14 views
0

私はMembershipDataContextでコードを使用して、Webフォームコードの背後にaspnet_userテーブルのユーザー名を変更しています。 しかし、MembershipDataContext名前空間が見つからないため、動作しません。 Google検索で検索結果はありません。MembershipDataContextについて名前空間/参照

おかげ

UPDATE:

public bool ChangeUserName(Guid userId, string newUserName) 
{ 
    bool success = false; 
    newUserName = newUserName.Trim(); 

    // Make sure there is no user with the new username 
    if (Membership.GetUser(newUserName) == null) 
    { 
     MembershipUser u = Membership.GetUser(userId); 
     string oldUsername = u.UserName; 
     // get current application 

     MembershipDataContext context = new MembershipDataContext(); 
     aspnet_User userToChange = (from user in context.aspnet_Users 
            where user.UserId == userId 
            select user).FirstOrDefault(); 

     if (userToChange != null) 
     { 
      userToChange.UserName = newUserName; 
      userToChange.LoweredUserName = newUserName.ToLower(); 

      context.SubmitChanges(); 

      // ASP.NET Issues a cookie with the user name. 
      // When a request is made with the specified cookie, 
      // ASP.NET creates a row in aspnet_users table. 
      // To prevent this sign out the user and then sign it in 

      string cookieName = FormsAuthentication.FormsCookieName; 
      HttpCookie authCookie = 
       HttpContext.Current.Request.Cookies[cookieName]; 

      FormsAuthenticationTicket authTicket = null; 

      try 
      { 
       authTicket = 
        FormsAuthentication.Decrypt(authCookie.Value); 

       FormsIdentity formsIdentity = 
        new FormsIdentity(
         new FormsAuthenticationTicket(
          authTicket.Version, 
          newUserName, 
          authTicket.IssueDate, 
          authTicket.Expiration, 
          authTicket.IsPersistent, 
          authTicket.UserData)); 

       string y = HttpContext.Current.User.Identity.Name; 
       string[] roles = 
        authTicket.UserData.Split(new char[] { '|' }); 
       System.Security.Principal.GenericPrincipal genericPrincipal = 
        new System.Security.Principal.GenericPrincipal(
                 formsIdentity, 
                 roles); 

       HttpContext.Current.User = genericPrincipal; 
      } 
      catch (ArgumentException ex) 
      { 
       // Handle exceptions 
      } 
      catch(NullReferenceException ex) 
      { 
       // Handle exceptions 
      } 

      FormsAuthentication.SignOut(); 
      HttpContext.Current.Session.Abandon(); 
      FormsAuthentication.SetAuthCookie(newUserName, false); 
      success = true; 
     } 
    } 

    return success; 
} 
+0

私たちを表示するには、 –

+0

私は以下を使用します:using System.Data.Linq; using System.Linq; –

+0

'MembershipDataContext'を右クリックし、" Resolve "の上にある矢印 –

答えて

2

あなたが参照する質問のコードは、MembershipDataContextというLinqからSQLエンティティのデータコンテキスト(これはDataContextの定義になります)を定義しています。 MembershipDataContextという名前のビルトインタイプはありません。

Here is another example同じものを行うプロジェクト(dbml定義を含む)です。

0

データコンテキストは、通常、LINQを指します。 Linqを使っていますか?その場合、Memberの部分がDBMLファイルのカスタム生成クラスであるため、DataContextactual classです。

こちらがお役に立てば幸いです。

+0

Webフォームで.net 3.5を使用しています。テンプレートが間違っていますか? –

関連する問題