IsUserInRole Error with SimpleMembership

2019-07-21 02:50发布

问题:

I use this codes for Role Authorization on my ASP.NET MVC 4 Internet Application..

@if (Roles.IsUserInRole("RolunAdi"))

{
    <a href="#">Test</a>
}

It works normally but sometimes I have an error.. It is on the this image..

http://forum.yazgelistir.com/JS/HTMLEditor/Image.aspx?id=593&siteid=0

I have 2 things in my mind.. First;

I have two connection string in web.config.. First for entity framework data model, second for SimpleMembership.. (By the way what can I do for use only one connection string??)

http://forum.yazgelistir.com/JS/HTMLEditor/Image.aspx?id=594&siteid=0

Second;

I create ASP.NET MVC 4 Internet Application.. After I have to set something for using ASP.NET SimpleMembership role..

回答1:

Do you have in system.web section within web.config....

<roleManager enabled="true" defaultProvider="SimpleRoleProvider">
  <providers>
    <clear/>
    <add name="SimpleRoleProvider" type="WebMatrix.WebData.SimpleRoleProvider, WebMatrix.WebData"/>
  </providers>
</roleManager>
<membership defaultProvider="SimpleMembershipProvider">
  <providers>
    <clear/>
    <add name="SimpleMembershipProvider" type="WebMatrix.WebData.SimpleMembershipProvider, WebMatrix.WebData" />
  </providers>
</membership>

Note: my roles look to DefaultConnection

<connectionStrings>
    <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=MyDB;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\MyDB.mdf" providerName="System.Data.SqlClient" />
</connectionStrings>

This appears to also be mentioned here Role based authentication in the new MVC 4 Internet template using simplemembership



回答2:

My problem is about InitializeSimpleMembership, because my all pages need login..

Normally AccountController class has it;

[Authorize]
[InitializeSimpleMembership]
public class AccountController : Controller
{
    ....

We need InitializeSimpleMembership all of our project.. So we create static class in App_Start folder: Exp;

InitializeSimpleMembershipProviderConfig.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WebMatrix.WebData;

namespace OsosPlus2.UI
{
    public class InitializeSimpleMembershipProviderConfig
    {
        public static void InitializeSimpleMembershipDbConnection()
        {
            if (!WebSecurity.Initialized)
            {
                WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);
            }
        }
    }
}

After we created class, we write this codes into Global.asax;

InitializeSimpleMembershipProviderConfig.InitializeSimpleMembershipDbConnection();

We have to delete this line under Filters folder

WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);

This answer translated from AliRıza Adıyahşi 's answer from yazgelistir.com

THANKS FOR HIS HELP AliRıza Adıyahşi