首页 > 代码库 > Asp.net Role manager tutorial

Asp.net Role manager tutorial

It is very useful in .net we can user framework provided role manager, and easily configure in Web.Config.

However, I find when I fullfil such configure: 

    <roleManager defaultProvider="QQmgsSqlProvider"
          enabled="true"
          cacheRolesInCookie="true"
          cookieName=".ASPROLES"
          cookieTimeout="30"
          cookiePath="/"
          cookieRequireSSL="true"
          cookieSlidingExpiration="true"
          cookieProtection="All" >
      <providers>
        <add
          name="QQmgsSqlProvider"
          type="System.Web.Security.SqlRoleProvider"
          connectionStringName="TwitterDbContext"
          applicationName="Twitter" />
      </providers>
    </roleManager>

  

When I send the Http request, I find the error:

{
  "Message": "An error has occurred.",
  "ExceptionMessage": "The Role Manager feature has not been enabled.",
  "ExceptionType": "System.Configuration.Provider.ProviderException",
  "StackTrace": " at System.Web.Security.Roles.EnsureEnabled()\r\n at Twitter.App.Controllers.APIControllers.PingController.Get() in C:\\Users\\Administrator\\Downloads\\Twitter-master\\Twitter-master\\QQmgs.App\\Controllers\\APIControllers\\PingController.cs:line 19\r\n at lambda_method(Closure , Object , Object[] )\r\n at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.b__9(Object instance, Object[] methodParameters)\r\n at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Controllers.ApiControllerActionInvoker.d__0.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Controllers.ActionFilterResult.d__2.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Controllers.AuthenticationFilterResult.d__0.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Dispatcher.HttpControllerDispatcher.d__1.MoveNext()"
}

  

So, after my investigation, I find there‘s one more thing I should do.

"It looks like your membership database is missing some required procedures required for the membership system.

You need to run VS command prompt and type this command "aspnet_regsql.exe" and link it to your database. This command will populate your database with all required database objects for the membership system."

refer to: https://forums.asp.net/t/2048614.aspx?Could+not+find+stored+procedure+dbo+aspnet_CheckSchemaVersion+

 

After I follow the instruction below, I find in my database there‘s some new tables like: "aspnet_Roles, aspnet_Membership".

 

Then I resend the Http request to test, succeeded !

Asp.net Role manager tutorial