我如何获得用户ID在ASP.Net的CreateUserWizard新注册的用户? [英] How do I get User ID for new registered user in ASP.Net CreateUserWizard?

查看:115
本文介绍了我如何获得用户ID在ASP.Net的CreateUserWizard新注册的用户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何获得用户ID在ASP.Net的CreateUserWizard新注册的用户?



我有一个页面,让新用户以创建用户向导注册。用户创建之后,我想使用新的用户的用户ID和电子邮件的客户详细信息表中插入一行。



我试图把在CreateUserWizard1_CreatedUser一些代码来获取用户ID。但是,从我了解到,用户在这一点上创建的;但是,用户不在这一点上登录。我得到的说......



的NullReferenceException是由用户代码处理的错误。
对象不设置到对象的实例。



如果我调试我可以看到,在客户的值为null时把一个断点。



从我对MSDN网站读取时,CreatedUser事件...
成员资格提供创建新网站的用户账号后发生。



我想之前创建用户向导更改行添加到客户详细信息表中显示继续按钮。在这一点上,用户可以去其他页面或关闭应用程序。



时,那里的CreatedUser后,其他一些事件,我可以把一些代码?
我看到有一个卸载事件。
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.createuserwizard_events%28v=vs.110%29.aspx



我能否能以某种方式记录在CreatedUser事件的用户,以获取用户ID?






下面是我不工作的注册页面上的代码。 字符串客户ID =

 保护无效CreateUserWizard1_CreatedUser(对象发件人,EventArgs五)$ B:对的代码行出现的错误$ b {
//添加客户的角色对新用户。
Roles.AddUserToRole((发件人为的CreateUserWizard).UserName,客户);

//获取当前用户的ID和电子邮件
字符串客户ID = Membership.GetUser(HttpContext.Current.User.Identity.Name).ProviderUserKey.ToString();
字符串email = Membership.GetUser(HttpContext.Current.User.Identity.Name).Email.ToString();
BOOL成功= CustomerDetailsAccess.CreateCustomerDetails(客户ID,电子邮件);



...

 } 


解决方案

当一个用户通过注册程序走, HttpContext.Current.User.Identity 将不被设置(匿名访问),除非用户登记已经登录



要解决这个问题,你需要从向导,而不是场景中获得的用户名。



要解决此问题,更改以下行:

 字符串客户ID = Membership.GetUser(HttpContext.Current.User.Identity.Name).ProviderUserKey.ToString(); 



这样:

 字符串客户ID = Membership.GetUser((发件人为的CreateUserWizard).UserName).ProviderUserKey.ToString(); 


How do I get User ID for new registered user in ASP.Net CreateUserWizard?

I have a page that allows new users to register with a Create User Wizard. Right after a user is created I would like to insert a row in a customer details table using the new user's User ID and email.

I tried putting some code in the CreateUserWizard1_CreatedUser to get the User ID. But, from what I learned, the user is created at this point; but, the user is not logged in at this point. I get an error that says...

"NullReferenceException" was handled by user code. Object not set to an instance of an object."

If I put a breakpoint in during debugging I can see that the value of the customerId is null.

From what I read on the MSDN site, the CreatedUser event... "Occurs after the membership provider has created the new Web site user account."

I want to add the row to the customer details table before the Create User Wizard changes to display the "Continue" button. At that point, the user can go to other pages or close the application.

Is there some other event after the CreatedUser where I can put some code? I see that there is an Unload event. http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.createuserwizard_events%28v=vs.110%29.aspx

Can I can somehow log in the user in the CreatedUser event to get the User ID?


Here is my code on the Register page that isn't working. The error occurs on the line of code: "string customerId ="

    protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e)
{
    // Add the Role of Customers to the new user.
    Roles.AddUserToRole((sender as CreateUserWizard).UserName, "Customers");

    // Get the current user's ID and email
    string customerId = Membership.GetUser(HttpContext.Current.User.Identity.Name).ProviderUserKey.ToString();
    string email = Membership.GetUser(HttpContext.Current.User.Identity.Name).Email.ToString();
    bool success = CustomerDetailsAccess.CreateCustomerDetails(customerId, email);

...

}

解决方案

When a user is going through the registration process, HttpContext.Current.User.Identity would not be set (anonymous access) unless the user registering is already logged in.

To solve the problem, you will need to get the username from the wizard instead of the context.

To fix the issue, change the following line:

string customerId = Membership.GetUser(HttpContext.Current.User.Identity.Name).ProviderUserKey.ToString();

to this:

string customerId = Membership.GetUser((sender as CreateUserWizard).UserName).ProviderUserKey.ToString();

这篇关于我如何获得用户ID在ASP.Net的CreateUserWizard新注册的用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆