Sue*_*ish 12 c# asp.net security visual-studio-2012
我有这个网站(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");
Run Code Online (Sandbox Code Playgroud)
谢谢!
小智 20
您应该使用WebSecurity.GetUserId(用户名)
WebSecurity.Login(username, password);
int currentuserid = WebSecurity.GetUserId(username);
Response.Redirect("~/Welcome.cshtml");
Run Code Online (Sandbox Code Playgroud)
看看这里:http://msdn.microsoft.com/en-us/library/webmatrix.webdata.websecurity.getuserid(v=vs.99)