0

I currently have my Authentication Token in my Base Controller. Because of this and using the Membership API, I am having to call all my Repos in my Controllers. I would like to have exposure to these repos in my models, but need this token available.

Is there a place I can set this token that will be updated on each request without affecting all users? Is there a way to expose this only enough that my View Models can access this in some base model and call repos with it?

Here's my current code in the Base Controller:

/// <summary>
/// Provide authentication credentials for the current user.
/// </summary>
protected IToken AuthenticatedUserToken
    {
        get
        {
            var userCred = (UsersCredential) this.Session [GlobalContext.UserCredentialsSessionKey];
            if ( userCred != null )
            {
                return userCred.UsersToken;
            }
            if (Request.IsAuthenticated)
            {
                var name = HttpContext.User.Identity.Name;
                var repository = new UserRepository(GlobalContext.ProvisioningApiServiceBase,
                                                               GlobalContext.WebServiceUserAuthenticationToken);
                var usersCredential = new UsersCredential
                {
                    UsersName = name,
                    UsersToken =
                        repository.GetAuthenticationTokenForAuthenticatedUser(name)
                };
                Session[GlobalContext.UserCredentialsSessionKey] = usersCredential;
                return usersCredential.UsersToken;
            }                
            return null;
        }
    }

protected RepositoryCollection _Repositories { get; set; }
protected RepositoryCollection Repositories
    {
        get
        {
            if (_Repositories == null)
            {
                _Repositories = new RepositoryCollection(this.AuthenticatedUserToken);
            }
            return _Repositories;
        }
    }
James Lee Baker
  • 797
  • 6
  • 9

2 Answers2

0

Change the access level of the property to public from protected. Protected methods and properties are only visible by the class and it's ancestors.

Heather
  • 2,602
  • 1
  • 24
  • 33
0

If i understood your question: It is better to update your model within action rather than provide access to repositories for your models. If you have some base model and you want to fill some property on almost each request and you don't want to copy-past code, you may use action filter that will be setting up this property for you. In this case you don't need to include any logic into your model classes. Something like this

Felix
  • 830
  • 10
  • 17