事实:
我的实施遵循this concept! + 请参阅下面的代码
一切运作良好
直到我将应用程序部署到
我遇到角色问题
无法正常工作。
以下是一些代码片段+调试输出
助手类
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");
}
}
的Global.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类和所有其他具有属性 [授权] 的类中 它使用此安装程序本地。
但是当我将应用程序部署到我的远程IIS时 它无法识别我以管理员身份登录(我已登录,但角色无法正常工作)。您可以在第二个屏幕截图中看到带有“admin”的UserData,但IsInRole方法失败了....
截图:
本地主机 http://s13.postimg.org/o9uqhlv6v/wi_local_glimpse_works.png
远程服务器 http://s9.postimg.org/pcavzvczj/wi_local_glimpse_works23.png
我缺少什么?任何人都遇到过同样的问题吗?
答案 0 :(得分:1)