【发布时间】:2018-07-06 12:58:25
【问题描述】:
我在互联网上搜索了一下,但找不到我的意思......
您能否详细说明我在这里做错了什么以及我如何才能真正完成我需要的事情?多个字符串下方的代码注释中解释了问题。
um.FindByName(username) - 当然给我一个错误"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);
}
}
}
}
更新:
好吧,如果我使用,一切正常:
var um = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
在我创建的新身份验证属性中......虽然不太确定使用 ApplicationDbContext() 和稍后创建的 Ado.net 数据模型的最佳实践是什么
【问题讨论】:
标签: c# asp.net authentication user-roles