gpt4 book ai didi

c# - ASP.NET Identity 2.0 Roles.IsUserInRole 给出 {"Invalid object name ' dbo.aspnet_SchemaVersions'."} 异常

转载 作者:行者123 更新时间:2023-11-30 15:27:59 25 4
gpt4 key购买 nike

我使用 ASP.NET Identity 2.0,我想确定登录的用户是否属于“admin”角色。在我的种子方法中,我创建了 3 个不同的角色,如下所示:

var store = new UserStore<ApplicationUser>(context);
var userManager = new ApplicationUserManager(store);
var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context));

string roleName = "admin";
if (!roleManager.RoleExists(roleName))
{
roleManager.Create(new IdentityRole(roleName));
}
roleName = "superuser";
if (!roleManager.RoleExists(roleName))
{
roleManager.Create(new IdentityRole(roleName));
}
roleName = "user";
if (!roleManager.RoleExists(roleName))
{
roleManager.Create(new IdentityRole(roleName));
}


var user = new ApplicationUser() { Email = "nsg@gmail.com", UserName = "Niclas" };
userManager.Create(user, "123456");
userManager.AddToRole(user.Id, "Admin");

取决于用户是否是管理员,他需要在导航栏中看到不同的链接,所以我编写了这段代码:

        <div class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
@Html.ActionLink("Lenio", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" })
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
@if (Roles.IsUserInRole(User.Identity.Name,"admin"))
{
<li>@Html.ActionLink("Admins", "Index", "Admins")</li>
<li>@Html.ActionLink("SuperUsers", "Index", "SuperUsers")</li>
<li>@Html.ActionLink("Users", "Index", "Users")</li>
<li>@Html.ActionLink("Areas", "Index", "Areas")</li>
<li>@Html.ActionLink("CDs", "Index", "CommunicationDevices")</li>
<li>@Html.ActionLink("Houses", "Index", "Houses")</li>
<li>@Html.ActionLink("Lendings", "Index", "Lendings")</li>
<li>@Html.ActionLink("Logs", "Index", "Logs")</li>
<li>@Html.ActionLink("Sensors", "Index", "Sensors")</li>
}
@if (User.IsInRole("user"))
{
<li>@Html.ActionLink("User", "Index", "Din bruger")</li>
}
<li>@Html.ActionLink("Home", "Index", "Home")</li>
<li>@Html.ActionLink("About", "About", "Home")</li>
<li>@Html.ActionLink("Contact", "Contact", "Home")</li>
</ul>
@Html.Partial("_LoginPartial")
</div>
</div>
</div>

问题是,当代码到达 @if (Roles.IsUserInRole(User.Identity.Name,"admin")) 部分时,我得到一个 {"Invalid object name ' dbo.aspnet_SchemaVersions'。” 异常。我的数据库中没有 aspnet_SchemaVersions 表,我也不需要它。这是与我拥有的身份相关的表格的图片: Identity Tables

数据库托管在 azure 上。

更新

DavidG 发布了一个解决初始问题的答案,但没有解决我无法使用的问题:

[Authorize(Roles = "admin")]

我需要能够使用这个属性,但我不能,因为我得到这个异常:

> [SqlException (0x80131904): Invalid object name 'dbo.aspnet_SchemaVersions'.]
System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +1789294
System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +5340642
System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) +244
System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) +1691
System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) +275
System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds) +1421
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite) +177
System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite) +208
System.Data.SqlClient.SqlCommand.ExecuteNonQuery() +163
System.Web.Util.SecUtility.CheckSchemaVersion(ProviderBase provider, SqlConnection connection, String[] features, String version, Int32& schemaVersionCheck) +392
System.Web.Security.SqlRoleProvider.CheckSchemaVersion(SqlConnection connection) +64
System.Web.Security.SqlRoleProvider.GetRolesForUser(String username) +753
System.Web.Security.RolePrincipal.IsInRole(String role) +9625099
System.Linq.Enumerable.Any(IEnumerable`1 source, Func`2 predicate) +146
System.Web.Mvc.AuthorizeAttribute.AuthorizeCore(HttpContextBase httpContext) +333
System.Web.Mvc.AuthorizeAttribute.OnAuthorization(AuthorizationContext filterContext) +379
System.Web.Mvc.ControllerActionInvoker.InvokeAuthorizationFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor) +143
System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__19(AsyncCallback asyncCallback, Object asyncState) +1680
System.Web.Mvc.Async.WrappedAsyncResult`1.CallBeginDelegate(AsyncCallback callback, Object callbackState) +59
System.Web.Mvc.Async.WrappedAsyncResultBase`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +151
System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate`1 endDelegate, Object tag, Int32 timeout) +94
System.Web.Mvc.Async.AsyncControllerActionInvoker.BeginInvokeAction(ControllerContext controllerContext, String actionName, AsyncCallback callback, Object state) +559
System.Web.Mvc.Controller.<BeginExecuteCore>b__1c(AsyncCallback asyncCallback, Object asyncState, ExecuteCoreState innerState) +82
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallBeginDelegate(AsyncCallback callback, Object callbackState) +73
System.Web.Mvc.Async.WrappedAsyncResultBase`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +151
System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object callbackState, BeginInvokeDelegate`1 beginDelegate, EndInvokeVoidDelegate`1 endDelegate, TState invokeState, Object tag, Int32 timeout, SynchronizationContext callbackSyncContext) +105
System.Web.Mvc.Controller.BeginExecuteCore(AsyncCallback callback, Object state) +588
System.Web.Mvc.Controller.<BeginExecute>b__14(AsyncCallback asyncCallback, Object callbackState, Controller controller) +47
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallBeginDelegate(AsyncCallback callback, Object callbackState) +65
System.Web.Mvc.Async.WrappedAsyncResultBase`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +151
System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object callbackState, BeginInvokeDelegate`1 beginDelegate, EndInvokeVoidDelegate`1 endDelegate, TState invokeState, Object tag, Int32 timeout, SynchronizationContext callbackSyncContext) +139
System.Web.Mvc.Controller.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state) +484
System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state) +50
System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__4(AsyncCallback asyncCallback, Object asyncState, ProcessRequestState innerState) +98
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallBeginDelegate(AsyncCallback callback, Object callbackState) +73
System.Web.Mvc.Async.WrappedAsyncResultBase`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +151
System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object callbackState, BeginInvokeDelegate`1 beginDelegate, EndInvokeVoidDelegate`1 endDelegate, TState invokeState, Object tag, Int32 timeout, SynchronizationContext callbackSyncContext) +106
System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +446
System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state) +88
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) +50
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +301
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

最佳答案

此错误的原因是您的应用程序尝试首先使用旧版本的身份进行连接。您不应使用对象的“旧”版本来管理角色。使用新的 UserManagerRoleManager 类。

例如:

var store = new UserStore<ApplicationUser>(context);
var userManager = new ApplicationUserManager(store);

if(userManager.IsInRole(userId, "NameOfRole")
{
//do stuff
}

要在您的 View 中使用它,您可以创建自己的助手:

namespace MyProject.MyNamespace.MyExtensions
{
public static class IdentityExtensions
{
public static bool IsInIdentityRole(this IPrincipal user, string role)
{
var userManager = GetUserManager(); //implement this!
return userManager.IsInRole(user.Identity.GetUserId(), role);
}
}
}

确保您的 View 了解此命名空间,将此行添加到您的 Views 文件夹中的 web.config 文件中(不是主要的 web.config!):

<add namespace="MyProject.MyNamespace.MyExtensions"/>

然后像这样在你的 View 中使用:

@if (User.IsInIdentityRole("admin"))
{
<li>@Html.ActionLink("Admins", "Index", "Admins")</li>
}

关于c# - ASP.NET Identity 2.0 Roles.IsUserInRole 给出 {"Invalid object name ' dbo.aspnet_SchemaVersions'."} 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26626534/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com