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;
}
}