问题描述
我在ASP.NET 5个简单的项目与一个注册的用户。我曾尝试通过扩展方法来获得当前登录用户的ID号 GetUserId()
从使用System.Security.Claims
命名空间。
不幸的是这个方法返回我不存在ID,我不知道为什么。
I have simple project in ASP.NET 5 with one registered user. I have tried to get id of current logged user by extension method GetUserId()
from using System.Security.Claims
namespace.Unfortunately this method returns me not existing id and i don't know why.
下面是我简单的code:
Here is my simple code:
var currentUserId = User.GetUserId();
方法的结果:
数据库:
推荐答案
很可能你得到的更多的老的身份验证票证依然有效,或者你得到一个身份验证票证其他网站正在运行本地。我会清除浏览器缓存和Cookie,然后再次运行它。
More than likely you're getting an old auth ticket that is still valid OR your getting an authentication ticket for another site your running locally. I would clear the browser cache and cookies and run it again.
另外,请尝试更改cookie的名称,默认情况下它设置为 .AspNet.Cookies
如果你是在本地运行多个应用这可能会导致问题。这抓住了我一会儿回来。
Also, Try changing the name of the cookie, by default it is set to .AspNet.Cookies
which can cause issues if you're running multiple apps locally. This caught me up a while back.
cookie的名称可以改变这样的
The cookie name can be changed like this
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
Provider = new CookieAuthenticationProvider
{
OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<UserManager, ApplicationUser>(
validateInterval: TimeSpan.FromMinutes(0),
regenerateIdentity: (manager, user) => manager.GenerateUserIdentityAsync(user))
},
CookieName = "myAuthCookie",
});
这是MSDN文档可能是这里找到。
The MSDN documentation on it can be found here.
这篇关于ASP.NET 5 MVC6 User.GetUserId()返回不正确的ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!