登录后直接从Websecurity获取CurrentUserID(C#/ASP.NET) [英] Getting the CurrentUserID from Websecurity directly after login (C#/ASP.NET)

查看:80
本文介绍了登录后直接从Websecurity获取CurrentUserID(C#/ASP.NET)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个网站(C#/ASP.NET),用户可以在该表格中注册一个帐户(这是VS11的默认模板),填写完所有内容并单击注册后,它将创建一个帐户并登录用户(效果很好).

I have this website (C#/ASP.NET) with a form where the user can register for an account (it is the default template of VS11) and after everything is filled in and the user clicks to register, it creates the account and logs in the user (which works great).

完成此步骤后,我想获取为其分配的UserID,但是它不起作用.我在此处放置了一个断点,以查看"currentuserid"和"WebSecurity.CurrentUserId"的值,但它们只有-1值.下一步是将用户重定向到下一页,然后在该页面上这些功能起作用.我以为我可以获取UserID,因为用户已经在我在此处提供的代码的第一行中登录了.

After this step I want to get the UserID which he was assigned, but it doesn't work. I've put a breakpoint there to see the values of both "currentuserid" and "WebSecurity.CurrentUserId" but they only have a -1 value. Next step is the user gets redirected to the next page, and on that page these functions work. I thought I would be able to get the UserID as the user has already gotten logged in in the first line of the code I provided here.

所以我的问题是,为什么它在这里不起作用?我对此不太满意,所以显然我缺少一些东西.

So my question is, why doesn't it work here? I am very noobish with this, so I am obviously missing something.

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");

谢谢!

推荐答案

您应该使用WebSecurity.GetUserId(username)

You should use WebSecurity.GetUserId(username)

WebSecurity.Login(username, password);
int currentuserid = WebSecurity.GetUserId(username);
Response.Redirect("~/Welcome.cshtml");

在这里看看:http://msdn.microsoft.com/zh-CN/library/webmatrix.webdata.websecurity.getuserid(v=vs.99)

这篇关于登录后直接从Websecurity获取CurrentUserID(C#/ASP.NET)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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