A Debian server is running a MVC4 app on Mono which uses a Membership ASPSqlProvider. The application has a method GetUserIdByName
which creates a link between the Users
table in the Membership database and the non-Membership Users
table Users
user's ID.
On my local Linux Mint system this setup does not have problem, but when the application is put on the Debian host (with the same XSP4 server and same version of Mono) and visited via the web it happens that MemberShip.GetUser()
returns null. I found that out by wrapping the code in try-catch blocks and reading the exception from the website:
public int GetCurrentUserId()
{
var userName = "";
var currentUserName = "test";
if (HttpContext.Current.User.Identity.IsAuthenticated)
{
try
{
userName = HttpContext.Current.User.Identity.Name;
currentUserName = Membership.GetUser().UserName;
}
catch (Exception ex)
{
throw new Exception(ex.Message + " User is:" + userName);
}
}
var currentUserId = _repository.GetUserIdByUserName(currentUserName);
return currentUserId;
}
The exception raised reads something like
NullReferenceException User is:theusersemailaddress@gmail.com
Interestingly enough when I query the database I see that the Username
is there in the Membership Users
table.
A solution is to just use the users email and change the method to GetUserIdByEmail
which I will do for now. Someone who provided this solution in another answer got told that this was not a real solution but a workaround, and I agree.
So the question remains: why does on the Debian machine MemberShip.GetUser()
return null while User.IsAuthenticated
returns true and the Username information is available in the database?
/Edit, I see now that HttpContext.Current.User.Identity.Name
returns the Email
column on the Debian machine, and the Username
column on the Linux mint one. Why would that be?