I was seeking on the internet a bit, but couldn't find exactly what I meant...
Could you please elaborate what exactly I'm doing wrong here and how can I actually accomplish what I need? Issue explained in code comment just below multiple strings.
um.FindByName(username)
- of course gives me an error "The entity type ApplicationUser is not part of the model for the current context"
public class MyNewAuthenticationAttribute : AuthorizeAttribute
{
public override void OnAuthorization(HttpActionContext actionContext)
{
if (actionContext.Request.Headers.Authorization == null)
{
base.OnAuthorization(actionContext);
}
else
{
string authenticationToken = actionContext.Request.Headers.Authorization.Parameter;
string decodedToken = Encoding.UTF8.GetString(Convert.FromBase64String(authenticationToken));
string[] usernamePasswordArray = decodedToken.Split(':');
string username = usernamePasswordArray[0];
string password = usernamePasswordArray[1];
// Here is the issue. I need to check whether the user is in admin role....
var um = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new WeatherAppDbEntities()));
var user = um.FindByName(username);
var isInRole = um.IsInRole(user.Id, "Admin");
if (// User is admin)
{
}
else
{
actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Unauthorized);
}
}
}
}
UPDATE:
Well it all works fine if i use:
var um = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
in the new authentication attribute that i've created... Not really sure though what's the best practice to use ApplicationDbContext()
with Ado.net data model created later