ASP.NET 5 MVC6 User.GetUserId()返回不正确的ID [英] ASP.NET 5 MVC6 User.GetUserId() returns incorrect id

查看:318
本文介绍了ASP.NET 5 MVC6 User.GetUserId()返回不正确的ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在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的名称可以改变这样的<​​/ P>

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文档可能是<一个href=\"https://msdn.microsoft.com/en-us/library/microsoft.owin.security.cookies.cookieauthenticationoptions.cookiename(v=vs.113).aspx#P:Microsoft.Owin.Security.Cookies.CookieAuthenticationOptions.CookieName\"相对=nofollow>这里找到。

The MSDN documentation on it can be found here.

这篇关于ASP.NET 5 MVC6 User.GetUserId()返回不正确的ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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