我在网上搜索了一下,但无法找到我的意思......
请您详细说明我在这里做错了什么,我怎样才能真正实现我的需要?在多个字符串下面的代码注释中解释了问题。
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数据模型的最佳做法
答案 0 :(得分:0)
ApplicationUser
身份似乎不是您当前上下文WeatherAppDbEntities
的一部分。您应该根据您的上下文实现它。
public class WeatherAppDbEntities : IdentityDbContext<ApplicationUser>
{
public WeatherAppDbEntities()
: base("DefaultConnection")
{
}
}
答案 1 :(得分:0)
就像Stormcloak提到的那样,你似乎有两个或更多DbContexts,因此你使用的是一个以上的数据库或连接字符串。当创建Asp.Net MVC项目时,tamplate还带有一些模型和控制器,以及作为名为&#34; DefaultConnection&#34; 的连接字符串。 Visual Studio使用SQL Server Express并使用连接字符串生成将存储有关用户信息的数据库,因此当您创建自己的数据库(&#34; WeatherAppDb&#34;)时,您基本上使用两个数据库。 如何防止这种情况?
1。创建MVC项目时检查<connectionStrings>
标记的Web.config文件,如果找到了什么
喜欢tihs
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-MyMVCProject-20180127104017.mdf;Initial Catalog=aspnet-MyMVCProject-20180127104017;Integrated Security=True"
providerName="System.Data.SqlClient" />
<add name="WeatherAppDbConnectionString" connectionString="Data Source=(LocalDb)\SQLEXPRESS;;Initial Catalog=WeatherAppDb;Integrated Security=True"
providerName="System.Data.SqlClient" />
最简单的方法是删除&#34;默认连接&#34;连接字符串并重命名您的&#34; Weather App DbConnectionString&#34;到&#34;默认连接&#34;,所以你只剩下这个
//renamed from WeatherAppDbConnectionString to Default Connection
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\SQLEXPRESS;;Initial Catalog=WeatherAppDb;Integrated Security=True"
providerName="System.Data.SqlClient" />
2。完成第一步后,请转到您的网站
WeatherAppDbEntities
和StormCloack声明确保你有&#34;默认连接&#34;这里
public class WeatherAppDbEntities : IdentityDbContext<ApplicationUser>
{
public WeatherAppDbEntities()
: base("DefaultConnection")
{
}
}
就您的代码而言,也许可能存在问题但不确定,我已经修改了一点。
WeatherAppDbEntities db = new WeatherAppDbEntities();
var um = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(db));
var user = db.Users.Find(username);