Asp.net mvc 2 在ASP.NET MVC 2应用程序中看不到主体上的角色
我正在编写一个ASP.NET MVC 2应用程序,不想使用ASP.NET成员资格。我确实想在控制器上使用Authorize属性。到目前为止我所做的是 Web.configAsp.net mvc 2 在ASP.NET MVC 2应用程序中看不到主体上的角色,asp.net-mvc-2,authorization,roles,iprincipal,Asp.net Mvc 2,Authorization,Roles,Iprincipal,我正在编写一个ASP.NET MVC 2应用程序,不想使用ASP.NET成员资格。我确实想在控制器上使用Authorize属性。到目前为止我所做的是 Web.config <roleManager enabled="true" /> <authentication mode="Forms"> <forms loginUrl="~/Authentication/Login" timeout="2880"/> </authentication>
<roleManager enabled="true" />
<authentication mode="Forms">
<forms loginUrl="~/Authentication/Login" timeout="2880"/>
</authentication>
<authorization>
<allow users="*" /> /* This is for testing */
</authorization>
我正在使用自定义身份,以便将来可以添加一些自定义属性。为了在我的控制器操作中测试这一点,我做了以下操作
var testPrincipal = User;
我可以看到所有用户信息的自定义标识,但主体对象上没有角色。对我错过的任何帮助都会很好。谢谢。我相信你需要一个角色提供者。与成员资格提供者处理用户成员资格的方式非常相似,创建、删除、验证、编辑要使用角色,您需要使用RoleProvider() 这还需要在web.config中启用角色,例如:
<roleManager enabled="enabled" defaultProvider="AspNetSqlRoleProvider">
<providers>
<clear/>
<add name="AspNetSqlRoleProvider"
type="System.Web.Security.SqlRoleProvider"
connectionStringName="ApplicationServices"
applicationName="/" />
<add name="AspNetWindowsTokenRoleProvider"
type="System.Web.Security.WindowsTokenRoleProvider"
applicationName="/" />
</providers>
</roleManager>
可能是:
更新:
最后,我通过改变工作环境来实现这一目标
protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{
var cookie = Context.Request.Cookies[FormsAuthentication.FormsCookieName];
if (cookie == null) return;
var decryptedCookie = FormsAuthentication.Decrypt(cookie.Value);
var roles = decryptedCookie.UserData.Split('|');
var tcmIdentity = new TcmIdentity(decryptedCookie.Name);
var tcmPrincipal = new GenericPrincipal(tcmIdentity, roles);
Context.User = tcmPrincipal;
}
到
埃里克,谢谢你的信息。我正在实现一个自定义角色管理器,这时我发现了一个将主体添加到当前线程的引用。一旦我做到了,我就很可爱。更新如下。Andrewawewesome,我建议将您的答案标记为答案,以保持未回答的问题与已回答的问题保持一致。
protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{
var cookie = Context.Request.Cookies[FormsAuthentication.FormsCookieName];
if (cookie == null) return;
var decryptedCookie = FormsAuthentication.Decrypt(cookie.Value);
var roles = decryptedCookie.UserData.Split('|');
var tcmIdentity = new TcmIdentity(decryptedCookie.Name);
var tcmPrincipal = new GenericPrincipal(tcmIdentity, roles);
Context.User = tcmPrincipal;
}
protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{
var cookie = Context.Request.Cookies[FormsAuthentication.FormsCookieName];
if (cookie == null) return;
var decryptedCookie = FormsAuthentication.Decrypt(cookie.Value);
var roles = decryptedCookie.UserData.Split('|');
var tcmIdentity = new TcmIdentity(decryptedCookie.Name);
var tcmPrincipal = new GenericPrincipal(tcmIdentity, roles);
Thread.CurrentPrincipal = Context.User = tcmPrincipal;
}