我有这个网站(C#/ASP.NET),其中有一个表单,用户可以在其中注册帐户(这是 VS11 的默认模板),填写完所有内容并且用户单击注册后,它会创建帐户并登录在用户中(效果很好)。
在此步骤之后,我想获取他分配的 UserID,但它不起作用。我在那里放置了一个断点来查看“currentuserid”和“WebSecurity.CurrentUserId”的值,但它们只有 -1 值。下一步是将用户重定向到下一页,并且在该页面上这些功能起作用。我认为我能够获得 UserID,因为用户已经在我在这里提供的代码的第一行中登录。
所以我的问题是,为什么它在这里不起作用?我对此很菜鸟,所以我显然错过了一些东西。
WebSecurity.Login(username, password);
int currentuserid = WebSecurity.CurrentUserId; // This doesn't work, only returns -1
<here I wish to update other tables but I need the user ID>
Response.Redirect("~/Welcome.cshtml");
Thanks!
您应该使用 WebSecurity.GetUserId(用户名)
WebSecurity.Login(username, password);
int currentuserid = WebSecurity.GetUserId(username);
Response.Redirect("~/Welcome.cshtml");
看看这里:http://msdn.microsoft.com/en-us/library/webmatrix.webdata.websecurity.getuserid(v=vs.99) http://msdn.microsoft.com/en-us/library/webmatrix.webdata.websecurity.getuserid(v=vs.99)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)