对不起我糟糕的英语。
事实:
- ASP.NET MVC3
- EF5。
- 表单认证
- 角色管理器已禁用
我的实现遵循这个概念!+请参阅下面的代码
一切正常
- 本地服务器 IIS Win7 (WebDeploy)
- 和我的旧 Windows Server 2008
直到我将应用程序部署到
- 新的 Windows Server 2008 Web
我对角色有疑问
- isInRole()和
- [授权(角色=“成员,管理员”)]属性
无法正常工作。
这是一些代码片段+调试输出
助手类
public static class UserHelper
{
public static bool IsAdmin(this ViewUserControl pg)
{
// @TODO Delete (Glimpse output)
string s = HttpContext.Current.User.IsInRole("admin") ? "UserHelper.IsAdmin() IsInRole() == true" : "UserHelper.IsAdmin() Application_AuthenticateRequest IsInRole() == false";
string b = pg.Page.User.IsInRole("admin") ? "UserHelper.IsAdmin() IsInRole() == true" : "UserHelper.IsAdmin() Application_AuthenticateRequest IsInRole() == false";
Trace.Write(s);
Trace.Write(b);
var id = HttpContext.Current.User.Identity as FormsIdentity;
Trace.Write("UserHelper.isAdmin(): UserData"+id.Ticket.UserData);
// ============================
return HttpContext.Current.User.IsInRole("admin");
}
}
全球.asax.cs
public class MvcApplication : HttpApplication
{
protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{
if (HttpContext.Current.User == null) return;
if (!HttpContext.Current.User.Identity.IsAuthenticated) return;
if (!(HttpContext.Current.User.Identity is FormsIdentity)) return;
var id = HttpContext.Current.User.Identity as FormsIdentity;
var userState = new UserState();
userState.FromString(id.Ticket.UserData);
HttpContext.Current.User = new GenericPrincipal(id, userState.Rollen.Split(new[] { ',' }));
// @TODO Delete (Glimpse output)
Trace.Write("Global.asax.cs -> Application_AuthenticateRequest Userdata: "+id.Ticket.UserData);
string s = HttpContext.Current.User.IsInRole("admin") ? "Global.asax.cs -> Application_AuthenticateRequest IsInRole() == true" : "Global.asax.cs -> Application_AuthenticateRequest IsInRole() == false";
Trace.Write(s);
}
AccountController.cs(示例)
[Authorize(Roles = "member,admin")]
[UserActive]
public ActionResult ChangePassword()
{
return View();
}
FormAuthService.cs
public class FormAuthService : IFormsAuthentication
{
public void Login(string userName, bool createPersistentCookie, IEnumerable<string> roles, int? userID = null)
{
var str = string.Join(",", roles);
var userData = new UserState
{
Benutzername = userName,
ID = userID.HasValue ? userID.Value : 0,
Rollen = str,
IsAdmin = str.Split(',').Contains("admin")
};
var authTicket = new FormsAuthenticationTicket(
1,
userName,
DateTime.Now,
DateTime.Now.AddDays(30),
createPersistentCookie,
userData.ToString(),
"/"
);
var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(authTicket));
if (authTicket.IsPersistent)
cookie.Expires = authTicket.Expiration;
HttpContext.Current.Response.Cookies.Add(cookie);
}
}
我正在用 Glimpse 调试我的代码。我想要实现的是第一个屏幕截图......
在我的UserHelper 类和具有属性[Authorize]的所有其他类中, 它在本地与此设置一起使用。
但是当我将应用程序部署到我的远程 IIS 时,它无法识别我以管理员身份登录(我已登录但角色不起作用)。您可以看到,在第二个屏幕截图中,带有“admin”的 UserData 在那里,但 IsInRole 方法失败了......
截图:
本地主机 http://s13.postimg.org/o9uqhlv6v/wi_local_glimpse_works.png
远程服务器 http://s9.postimg.org/pcavzvczj/wi_local_glimpse_works23.png
我错过了什么?有人遇到过同样的问题吗?