我在 ASP.NET 5 中有一个简单的项目,只有一个注册用户。我尝试通过扩展方法获取当前登录用户的IDGetUserId()
from using System.Security.Claims
命名空间。
不幸的是,这个方法返回给我不存在的ID,我不知道为什么。
这是我的简单代码:
var currentUserId = User.GetUserId();
方法结果:
数据库:
您很可能会得到一个old仍然有效的身份验证票证,或者您获得本地运行的另一个站点的身份验证票证。我会清除浏览器缓存和cookie,然后再次运行它。
另外,尝试更改 cookie 的名称,默认情况下它设置为.AspNet.Cookies
如果您在本地运行多个应用程序,这可能会导致问题。不久前这让我很困惑。
cookie名称可以这样修改
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 文档可以是在这里找到 https://msdn.microsoft.com/en-us/library/microsoft.owin.security.cookies.cookieauthenticationoptions.cookiename(v=vs.113).aspx#P:Microsoft.Owin.Security.Cookies.CookieAuthenticationOptions.CookieName.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)