سؤال

Hi i`m having trouble with a website. Sometimes after the Login my Site redirects me to the Login-Page saying i´m not having the rights to use this function, please login.

Ok first i thought, somethign wrong with my Accesrights but thats not, if i just go back one site in the Browser and send the Request again, it works.

So i debugged it, and after a ton of tests i got a result finding out, that when i create my Service Context with Ninject the UserName is an empty string.

Here is my Code: First my Interface IServiceContext:

public interface IServiceContext
{
    string UserName { get; }
}

Then the Class ServiceContext:

public class ServiceContext : IServiceContext
{
    private static Object _syncRoot = new Object();
    private static long _instance = 0;
    private long _currentInstance = 0;

    public string UserName { get; private set; }

    public ServiceContext()
    {
        lock (_syncRoot)
        {
            if (_instance >= long.MaxValue)
                _instance = 0;
            _currentInstance = _instance++;
        }

        if (System.Web.HttpContext.Current.User == null)
            UserName = "";
        else
            UserName = System.Web.HttpContext.Current.User.Identity.Name;
    }

    public ServiceContext(string userName)
    {
        UserName = userName;
    }


    public override string ToString()
    {
        return string.Format("#{0}, UserName: {1}", _currentInstance, UserName);
    }
}

My Binding is set in a seperate file, looks like:

Bind<SecurityManagement >().ToSelf().InTransientScope();
Rebind<IServiceContext>().To<ServiceContext>().InRequestScope();

I need to use rebind, cause in my framework a StandardBinding for serviceContext is made.

And the Call from my InvokeMethod:

class SecurityManagement : IInterceptor
{
    public void Intercept(IInvocation invocation)
    {
        IServiceContext _context = _kernel.Get<IServiceContext>();
        String name = System.Web.HttpContext.Current.User.Identity.Name;
    }
}

So Sometimes it happens, that my _context.UserName is an empty String. I found out that the System.Web.HttpContext.Current.User.Identity.Name property is an Empty string whenn Injecting into ServiceContext, but the Variable Name has the right Username. It is no Option for me to make the setter for the Property UserName public cause of the framework i´m using.

So anybody an idea why this is happening? perhaps any idea to solve the problem

هل كانت مفيدة؟

المحلول

Accessing straight to System.Web.HttpContext inside Service Layer and Repository Layer is not a good practice, because it is hard to unit test your project.

Instead, you want to inject HttpContextBase via constructor.

public class ServiceContext : IServiceContext
{ 
   HttpContextBase _httpContextBase,

   public ServiceContext(HttpContextBase httpContextBase)
   {
      _httpContextBase = httpContextBase;
   }
}

Then access user like this -

string username = _httpContextBase.User.Identity.Name;
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top