Şu anda bir ASP.NET MVC2 web uygulamasında bazı özel güvenlik uygulamaya çalışıyorum.Özel IIdentity ve IPcincincal MVC2'de FormsAuthenticationTicket çerezi kullanarak
benim denetleyicisi eylemleri birinde [Authorize(Roles="Admins")]
özelliğini kullanırsanız gösterileri altında benim kod olarak ama nedense gerçekten basit bir şey yapmak çalışıyorum, Context.User.IsInRole("Admins")
veya Page.User.IsInRole("Admins")
her zaman yanlıştır kontrol ediniz.
User.Identity.Name
'un da boş olması da garip.
Aşağıdaki koduma bakın, daha sonra GenericPrincipal
nesnesinde Context.User öğesini ayarlamak için Gloabl.asax içinde Application_AuthenticateRequest
olay tanıtıcısında kullandığım bir çerez içinde FormsAuthenticationTicket kullanıyorum.
My giriş kodu:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Login(string username, string password)
{
//this would obviously do a check against the supplied username and password
if (true)
{
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, username, DateTime.Now,
DateTime.Now.AddMinutes(15), false, "Admins|Users|Members");
string encTicket = FormsAuthentication.Encrypt(ticket);
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket);
this.Response.Cookies.Add(cookie);
string url = FormsAuthentication.GetRedirectUrl(username, false);
Response.Redirect(url);
}
return View();
}
Benim Global.asax Kodu:
void MvcApplication_AuthenticateRequest(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
HttpContext context = application.Context;
var cookie = context.Request.Cookies[FormsAuthentication.FormsCookieName];
if (cookie != null)
{
// Get the authentication ticket
// and rebuild the principal & identity
FormsAuthenticationTicket authTicket =
FormsAuthentication.Decrypt(cookie.Value);
string[] roles = authTicket.UserData.Split(new Char[] { '|' });
GenericIdentity userIdentity = new GenericIdentity(authTicket.Name);
GenericPrincipal userPrincipal =
new GenericPrincipal(userIdentity, roles);
context.User = userPrincipal;
}
Ben izle penceresinde görebilirsiniz yukarıda context.User ayarlayabilir ve nesne mükemmel ayarlandıktan sonra, Doğru isim vb. ile doğru rollerde, ancak kontrolör eylemlerini kilitler ve kilitlerseniz veya sitemdeki herhangi bir yerden Müdür'ü kullanırsanız, her zaman bir rol atanmamış bir boş dizeye ayarlanır!
Sanırım gerçekten aptalca bir şey yapıyorum ama eğer birisi bunu gösterebilirse gerçekten çok memnun olurum. Size yeni UserPrincipal atama
u gönderin olabilir web.config ayarları lütfen FORMLAR? –
Ben aynı sorunu yaşıyorum – Mohsen