IsUserInRole error with SimpleMembership

I am using these codes to authorize a role in my ASP.NET MVC 4 internet application.

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

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

      

It works fine, but sometimes I have a bug .. It is in 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 strings in web.config. First for the entity entity data model, second for SimpleMembership .. (By the way, what can I do to use only one connection string?)

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

Secondly,

I am creating an ASP.NET MVC 4 web application. After I need to install something to use the ASP.NET SimpleMembership role.

+2


source to share


2 answers


My problem is in InitializeSimpleMembership because I need all pages to login.

Usually the AccountController class has one;

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

      

We need InitializeSimpleMembership for our entire project. Therefore, we create a static class in the 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 creating the class, we write these codes to Global.asax;

InitializeSimpleMembershipProviderConfig.InitializeSimpleMembershipDbConnection();

      

We have to delete this line in the Filters folder

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

      

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

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

+1


source


You have in the system.web section in your 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 are related 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 is also mentioned here Role Based Authentication in the new MVC 4 internet pattern using simplemembership

+1


source







All Articles