2011-10-21 28 views
5

我在我的項目中使用成員API與OpenId實現在我的項目構建於MVC2框架。 除了用戶名外,我還需要在註冊時與用戶關聯的其他字段。在aspnet_profile表中存儲額外的數據與會員資料數據

雖然我不確定,但我認爲asp.net中的Profile系統是爲這種類型的需求而構建的。另外,我還看到一張名爲'aspnet_profile'的其他成員表。

我在web.config中的應用添加以下啓用配置文件:

<profile enabled="true"> 
     <properties> 
     <add name="FullName" allowAnonymous="false"/> 

     </properties> 

    </profile> 

正如前面所說,應用程序需要一些額外的數據與用戶相關聯,採用會員API創建用戶,所以當我加的代碼幾行製作進入配置表

System.Web.Security.MembershipCreateStatus status = MembershipService.CreateUser(userModel.UserName, userModel.Password, userModel.UserName); 

        if (status == System.Web.Security.MembershipCreateStatus.Success) 
        { 
         FormsService.SignIn(userModel.UserName, true); 
         Session["Username"] = userModel.UserName; 

         dynamic profile = ProfileBase.Create(MembershipService.GetUser(userModel.UserName).UserName); 
         profile.FullName = userModel.UserFullName; 
         profile.Save(); 

         RedirectToAction("Tech", "Home"); 



        } 

但我沒有看到aspnet_profile表加入數據庫中的所有行。另外,我想問,如果這是默認的會員數據

回答

4

我把它通過使相關的web.config文件默認配置文件提供程序名稱一些變化工作:

<profile enabled="true" defaultProvider="AspNetSqlProfileProvider"> 
     <providers> 
     <clear/> 
     <add name="AspNetSqlProfileProvider" applicationName="/" connectionStringName="ApplicationServices" type="System.Web.Profile.SqlProfileProvider" /> 
     </providers> 

     <properties> 
     <add name="FullName" allowAnonymous="false"/> 

     </properties> 

    </profile> 

此外,我添加的呼叫之間的一個多線ProfileBase.Create功能和設置Profile.FullName ;

profile.Initialize(userModel.userName, true); 

我終於看到了aspnet_profile表爲新註冊用戶:)

+0

非常有幫助,非常感謝! –

1

1沿增加額外數據的首選方式,你需要創建一個配置文件類定義模板結構

2,你需要配置你的在web.config中的配置文件設置爲

3,現在你現在可以使用你的代碼。

在使用它之前,您只需要執行前兩個步驟即可。

裁判:http://weblogs.asp.net/jgalloway/archive/2008/01/19/writing-a-custom-asp-net-profile-class.aspx

+0

我不想創建一個單獨的配置文件類此的條目。我想我缺少web.config中的設置。我不認爲有必要創建一個配置文件類來使這個工作 –

1

在使用ASP.NET配置文件提供者,profile properties是自定義的用戶配置文件的方式。但是,您不必自己創建和保存配置文件實例 - 它是通過ASp.NET運行時完成的。使用HttpContext.Current.Profile或從其自身的頁面使用強類型的動態Profile屬性。通過以後使用,您可以編寫代碼,如

Profile.UserName = "User Name"; 

有沒有必要調用Save方法。欲瞭解更多信息,請參閱this article

在Web應用程序,一個不能確指動態創建的Profile類,所以你有HttpContext.Current.Profile工作(當然你也可以把它分配給一個變量dynamic更多可讀的代碼由你完成)。另一種方法是寫your own class

+0

MVC不會給你HttpContext.Current,你可以直接使用HttpContext.Profile訪問配置文件類 –

+0

@Kunal,'HttpContext.Profile'是一個實例屬性,所以你需要一個HttpContext實例,它通常在任何ASP.NET應用程序(包括ASP.NET MVC)中都可以通過'HttpContext.Current'獲得! – VinayC

+0

@VinayC他的意思是說,在一個MVC'Controller'或'View'類中,有一個'HttpContext'屬性,它基本上是'HttpContext.Current'的一個包裝。 –