I am new to MVC3 and I am trying to implement customer user and role Authentication. I am able to validate the user using my custom database but now I want to use my custom roles out of the same table. I followed the instructions from this link below to get this far:
MVC Custom Roles and Validation
The tutorial had me put this block of code in my global.asax file.
protected void FormsAuthentication_OnAuthenticate(Object sender, FormsAuthenticationEventArgs e)
{
if (FormsAuthentication.CookiesSupported == true)
{
if (Request.Cookies[FormsAuthentication.FormsCookieName] != null)
{
try
{
//let us take out the username now
string roles = string.Empty;
string username = FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value).Name;
using (RecipeEntities entities = new RecipeEntities())
{
**-->> User user = entities.KeyFobUsers.Single(u => u.username == username);
roles = user.AccessLevel.Trim();
}
//let us extract the roles from our own custom cookie
//Let us set the Pricipal with our user specific details
e.User = new System.Security.Principal.GenericPrincipal(new System.Security.Principal.GenericIdentity(username, "Forms"), roles.Split(';'));
}
catch
{
//something went wrong
}
}
}
}
Currently in the script above MVC has 'User' underlined stating that 'System.Web.HttpApplication.User' is a 'property' but is used like a 'type'
I have googled around and tried the links I found on Stack but nothing was geared towards my issue. I am just trying to get the role out of my custom database table into the Roles of MVC so I can use permissions on certain windows.
Does anyone have any suggestions or want to push me in the right direction?
The compiler is conflicted with the User
property of the HttpApplication and the User
class. You can use var
instead to avoid the conflict:
var user = entities.KeyFobUsers.Single(u => u.username == username);
Or fully qualify the User
class with the namespace it's in:
MyNamespace.User user = ...