简体   繁体   English

登录后c#owin无法获取User.Identity

[英]c# owin can't get User.Identity after signin

After i finish my login code 完成登录代码后

        var identity = new ClaimsIdentity(claims, OAuthConfigur.AuthenticationType);

        this.AuthenticationManager.SignIn(new AuthenticationProperties
        {
            ExpiresUtc = DateTimeOffset.Now.AddMinutes(30),
            IsPersistent = false
        }, identity);

        return RedirectToAction("Index", "Home");

After RedirectToAction , there is the cookie in broswer. 在RedirectToAction之后,浏览器中有cookie。
But when Authorize attribute there is no Authorize. 但是,当Authorize属性没有授权时。
In my custom Authorize actionfilter , 在我的自定义“授权”操作过滤器中,

httpContext.User.Identity.IsAuthenticated

always return false. 总是返回false。

I find a way to get identity below: 我在下面找到一种获得身份的方法:

    private ClaimsIdentity GetIdentity(HttpContextBase httpContext)
    {
        var ticket = httpContext.GetOwinContext().Authentication
                .AuthenticateAsync(OAuthConfigur.AuthenticationType).Result;
        var identity = ticket != null ? ticket.Identity : null;
        return identity;
    }

after this function, i can get the useridenttity. 使用此功能后,我可以获得用户身份。

Is this correct?? 这个对吗??

If i need users login info , i need call this function everytime is action? 如果我需要用户登录信息,则每次执行操作时都需要调用此函数?

Thank you reply! 谢谢回复!

Here's my Startup.cs 这是我的Startup.cs

 public void ConfigureAuth(IAppBuilder app)
    {
        // Enable Application Sign In Cookie
        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = OAuthConfigur.AuthenticationType,
            AuthenticationMode = AuthenticationMode.Passive,
            LoginPath = new PathString(OAuthPaths.LoginPath),
            LogoutPath = new PathString(OAuthPaths.LogoutPath),
            ExpireTimeSpan = TimeSpan.FromMinutes(20)
        });

        // Setup Authorization Server
        app.UseOAuthAuthorizationServer(new CustomerOAuthAuthorizationServerOptions());
    }

Just in case someone stumbles upon this in the future. 以防万一将来有人偶然发现这一点。 I had the same issue and I was pulling my hair out when I realised that I had set the 我遇到了同样的问题,当我意识到自己已经设置好

CookieSecure = CookieSecureOption.Always

on the CookieAuthenticationOptions class :/ 在CookieAuthenticationOptions类上:/

So obviously cookies were only access over https and because my local environment was not setup with https (It used to be) it could not read the cookie. 因此很明显,cookie只能通过https访问,并且因为我的本地环境未使用https设置(过去是),因此无法读取cookie。

I have one scenario when published the application to Production server the call httpContext.GetOwinContext().Authentication .AuthenticateAsync("Application") always return null in IE browser. 在将应用程序发布到生产服务器时,我遇到一种情况,调用httpContext.GetOwinContext()。Authentication .AuthenticateAsync(“ Application”)在IE浏览器中始终返回null。 For this case, go to IE browser Internet Options -> Trusted sites, add your identity server application url as trusted site. 对于这种情况,请转到IE浏览器的Internet选项->可信站点,将您的身份服务器应用程序URL添加为可信站点。 System works then. 系统即可正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM