我在ASP.NET MVC 4 Internet Application上使用此代码进行角色授权..
@if (Roles.IsUserInRole("RolunAdi"))
{
<a href="#">Test</a>
}
它正常工作但有时我有错误..它在这张图片上..
http://forum.yazgelistir.com/JS/HTMLEditor/Image.aspx?id=593&siteid=0
我脑子里有两件事。第一件事;
我在web.config中有两个连接字符串..第一个用于实体框架数据模型,第二个用于SimpleMembership ..(顺便说一句,我只能使用一个连接字符串?)
http://forum.yazgelistir.com/JS/HTMLEditor/Image.aspx?id=594&siteid=0
第二
我创建了ASP.NET MVC 4 Internet Application ..我必须为使用ASP.NET SimpleMembership角色设置一些东西..
答案 0 :(得分:1)
你在web.config中的system.web部分吗?
<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>
注意:我的角色看起来是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>
这里似乎也提到Role based authentication in the new MVC 4 Internet template using simplemembership
答案 1 :(得分:1)
我的问题是关于InitializeSimpleMembership,因为我的所有页面都需要登录..
通常AccountController类有它;
[Authorize]
[InitializeSimpleMembership]
public class AccountController : Controller
{
....
我们需要所有项目的InitializeSimpleMembership。所以我们在App_Start文件夹中创建静态类: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);
}
}
}
}
在我们创建了类之后,我们将这些代码写入Global.asax;
InitializeSimpleMembershipProviderConfig.InitializeSimpleMembershipDbConnection();
我们必须删除过滤器文件夹
下的这一行WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);
此答案来自AliRıza Adıyahşi
的yazgelistir.com答案感谢他的帮助 AliRıza Adıyahşi