对不起我的英语不好。表单身份验证User.IsInRole(),授权属性 - 在生产服务器上不工作
的事实:
- ASP.NET MVC3
- EF5。
- FormAuthentication
- roleManager禁用
我的实现如下this concept! + 见代码如下
工作一切良好,上
- 本地服务器IIS的Win7(WebDeploy)
- &我的旧的Windows Server 2008的
,直到我部署的应用程序到
- 新的Windows Server 2008网络
我与角色
- 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");
}
}
的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);
}
}
我调试我的代码以一瞥。 我试图做到的,是第一张截图...
在我UserHelper类和所有其他类与属性[授权] 它的工作原理本地这个设置。
但是,当我将应用程序部署到我的远程IIS 它不能识别我作为管理员登录(我登录但角色不工作)。可以看到,在第二个屏幕中的UserData与 “管理” 是有,但IsInRole方法失败....
截图:
本地主机 http://s13.postimg.org/o9uqhlv6v/wi_local_glimpse_works.png
远程服务器 http://s9.postimg.org/pcavzvczj/wi_local_glimpse_works23.png
我失踪了什么?任何人都遇到同样的问题?
这可能很明显,但您是否检查远程数据库以确保您的用户表和行的角色存在?这可能与创建表格和数据一样简单。 –
嗨,数据库应该没问题。我在本地环境中使用与我的生产服务器上相同的(远程)数据库。我检查了两次。 – kkern