asp.netメンバーシッププロバイダーによって提供される情報とは別に、createuserwizardを使用して追加のユーザー情報をmysqlデータベースに格納しています。この情報を「userprofile」という別のテーブルに保存したいと思います。'userprofile'のユーザーIDはint型であり、'my_aspnet_usersの'id'を参照する外部キーです。
これが私の問題です。新しく作成されたユーザーのユーザーIDを取得して、「userprofile」テーブルに保存できないようです。これらの行は機能しません:
MembershipUser newUser = Membership.GetUser(RegisterUser.UserName);
Int32 newUserId = (Int32)newUser.ProviderUserKey;
これらの行により、usernameがProviderUserKeyによって返されるguid値に設定されますが、これは非常に奇妙です。一方、my_aspnet_usersのid列は、ユーザーの作成時に1ずつ増加します。そして、「userprofile」テーブルには何も追加されません。
ここに上記のコードを挿入しました。
protected void RegisterUser_CreatedUser(object sender, EventArgs e)
{
FormsAuthentication.SetAuthCookie(RegisterUser.UserName, false /* createPersistentCookie */);
string continueUrl = RegisterUser.ContinueDestinationPageUrl;
if (String.IsNullOrEmpty(continueUrl))
{
continueUrl = "~/";
}
// Retrieve all the values from the registration form to insert into database
TextBox FirstNameTextBox = (TextBox)RegisterUserWizardStep.ContentTemplateContainer.FindControl("FirstName");
TextBox LastNameTextBox = (TextBox)RegisterUserWizardStep.ContentTemplateContainer.FindControl("LastName");
TextBox UsernameTextBox = (TextBox)RegisterUserWizardStep.ContentTemplateContainer.FindControl("UserName");
TextBox EmailTextBox = (TextBox)RegisterUserWizardStep.ContentTemplateContainer.FindControl("Email");
// Get the UserId of the just-added user
MembershipUser newUser = Membership.GetUser(RegisterUser.UserName);
Int32 newUserId = (Int32)newUser.ProviderUserKey;
MySqlConnection DBConn = new MySqlConnection(WebConfigurationManager.ConnectionStrings["LocalMySqlServer"].ConnectionString);
MySqlCommand DBCmd = new MySqlCommand();
try
{
// Add Insert statement to insert into database
DBCmd = new MySqlCommand(
"INSERT INTO userprofile(UID, Email, Fname, Lname)" +
"VALUES (@UID, @Email, @Fname, @Lname)", DBConn);
// Add database parameters
DBCmd.Parameters.Add("@UID", MySqlDbType.Int32).Value = newUserId;
DBCmd.Parameters.Add("@Email", MySqlDbType.VarChar).Value = EmailTextBox.Text;
DBCmd.Parameters.Add("@Fname", MySqlDbType.VarChar).Value = FirstNameTextBox.Text;
DBCmd.Parameters.Add("@Lname", MySqlDbType.VarChar).Value = LastNameTextBox.Text;
DBCmd.ExecuteNonQuery();
}
catch(Exception exp){
Response.Write(exp);
}
// Close database connection and dispose database objects
DBCmd.Dispose();
DBConn.Close();
DBConn = null;
Response.Redirect(continueUrl);
}
追加情報を得るためにmysqlとcreateuserwizardを使用した人はいますか?
どんな助けでもいただければ幸いです。ありがとう!