User.Identity.IsAuthenticated是成功登录后假 [英] User.Identity.IsAuthenticated is false after successful login
问题描述
我需要一个成功登录后直接获取用户标识GUID。下面code不工作:
I need to get the UserId Guid directly after a successful login. The following code doesn't work:
if (Membership.ValidateUser(txtUsername.Value, txtPassword.Value))
{
FormsAuthentication.SignOut();
FormsAuthentication.SetAuthCookie(txtUsername.Value, true);
if (HttpContext.Current.User.Identity.IsAuthenticated)
{
// doesn't run
Guid puk = (Guid)Membership.GetUser().ProviderUserKey;
}
}
以下code不工作:
The following code does work:
if (Membership.ValidateUser(txtUsername.Value, txtPassword.Value))
{
FormsAuthentication.SignOut();
FormsAuthentication.SetAuthCookie(txtUsername.Value, true);
MembershipUser user = Membership.GetUser(txtUsername.Value);
if (user != null)
{
Guid puk = (Guid)user.ProviderUserKey;
}
}
为什么会出现这种情况?是有什么更多的事情要做,除了 SetAuthCookie
?
推荐答案
由于当你调用 FormsAuthentication.SetAuthCookie(txtUsername.Value,真实);
您存储的关键客户端的Cookie。为此,您需要做的用户的响应。
和 HttpContext.Current.User.Identity
来充满饼干你需要多一个请求。
Because when you call FormsAuthentication.SetAuthCookie(txtUsername.Value, true);
you store the key on the client's cookies. For this you need to do a response to the user.
And for HttpContext.Current.User.Identity
to be filled with cookie you need one more request.
在短期你的计划是这样的:
In short your scheme looks like this:
-
客户端发送他的用户名和密码。
Client sends his UserName and Password.
服务器获取并检查它。如果它们是有效的服务器发送设置Cookie
头给客户端。
Server gets and checks it. If they are valid the server sends Set-Cookie
header to the client.
客户端接收并存储。对于每个请求客户机发送的cookie回服务器
Client receives and stores it. For each request client sends cookies back to the server.
这篇关于User.Identity.IsAuthenticated是成功登录后假的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!