locked
Could you please help me in converting the below code snippet to .NET Core RRS feed

  • Question

  • User2056817590 posted

    public static LBCTUser user {
                get {
                    //if (HttpContext.Current.Response.RedirectLocation == null){
                    //    HttpContext.Current.Response.Redirect("~/Login/LoginTimeout");
                    //    return null;
                    //}

                    if (LBCTWebSecurity.SharedInstance.User != null) {
                        return LBCTWebSecurity.SharedInstance.User;
                    } else {
                        HttpContext.Current.Response.Redirect("~/Login/LoginTimeout");
                        return null;
                    }
                    
                }
            }

    Wednesday, April 7, 2021 8:12 AM

All replies

  • User1686398519 posted

    Hi kalvakrishna, 

    Do you want to access HttpContext in ASP.NET Core?

    If this is the case, the recommended method is to use the built-in dependency injection container to register dependencies.

    Access HttpContext in ASP.NET Core

    Startup

    public void ConfigureServices(IServiceCollection services)
    {
       services.AddHttpContextAccessor();
    }

    Class

        public class TestModel1
        {
            private readonly IHttpContextAccessor _httpContextAccessor;
            public TestModel1(IHttpContextAccessor httpContextAccessor)
            {
                _httpContextAccessor = httpContextAccessor;
            }
            public LBCTUser user
            {
                get
                {
                    if (LBCTWebSecurity.SharedInstance.User != null)
                    {
                        return LBCTWebSecurity.SharedInstance.User;
                    }
                    else
                    {
                        _httpContextAccessor.HttpContext.Response.Redirect("~/Login/LoginTimeout");
                        return null;
                    }
                }
            }
        }

    Best Regards,

    YihuiSun

    Thursday, April 8, 2021 1:57 AM