En iyi yanıtlayıcılar
FormsAuthentication Hakkında

Soru
-
Merhaba bir mvc projesinde birkaç Controller kullanıyorum. Ben bu iki Cotroller'a ayrı ayrı FormsAuthentication yapıyorum ancak PersonController'a göre işlem yapıyor. Aynı zamanda SatisController içinde FormsAuthentication yapabilir miyim
Örnek Kodum. Burada Satis ve Person Adı altında iki adet Controller var ve ben bu Controller'lara erişim yetkisi olan kişilerin projye erişmesini istiyorum
public void Erisimİslemleri(string AdminValue) { UserInformation ui = new UserInformation(); if (ui.Yetki(2).Yonetim == true | ui.Yetki(2).Okuma == true) { var ticket = new FormsAuthenticationTicket( 1, AdminValue, DateTime.Now, DateTime.Now.AddMinutes(30), true, "Satis", FormsAuthentication.FormsCookiePath); string hash = FormsAuthentication.Encrypt(ticket); HttpCookie cokie = new HttpCookie(FormsAuthentication.FormsCookieName, hash); cokie.Expires = ticket.Expiration; Response.Cookies.Add(cokie); } if (ui.Yetki(1).Yonetim == true | ui.Yetki(1).Okuma == true) { var ticket = new FormsAuthenticationTicket( 1, AdminValue, DateTime.Now, DateTime.Now.AddMinutes(30), true, "Person", FormsAuthentication.FormsCookiePath); string hash = FormsAuthentication.Encrypt(ticket); HttpCookie cokie = new HttpCookie(FormsAuthentication.FormsCookieName, hash); cokie.Expires = ticket.Expiration; Response.Cookies.Add(cokie); } }
Satis Controller
[Authorize(Roles = "Satis")] public class SatisController : Controller {
Person Controller
[Authorize(Roles = "Person")] public class PersonController : Controller {
Üretmek en büyük icraatır
- Düzenleyen Musa adsız 27 Ocak 2019 Pazar 19:33
Yanıtlar
-
global.asax dosyasında,
protected void Application_AuthenticateRequest(Object sender, EventArgs e) { if (HttpContext.Current.User != null) { if (HttpContext.Current.User.Identity.IsAuthenticated) { if (HttpContext.Current.User.Identity is FormsIdentity) { FormsIdentity id = (FormsIdentity)HttpContext.Current.User.Identity; FormsAuthenticationTicket ticket = id.Ticket; string userData = ticket.UserData; string[] roles = userData.Split(';'); HttpContext.Current.User = new GenericPrincipal(id, roles); } } } }
kullanabilirsiniz.
NOT: web.config dosyanızı, form yetkilendirme kurallarına göre yapılandırdığınızı varsayıyorum.
Sorunuzun yanıtı bu ise "Yanıt olarak işaretle"yerek siz de forumun işleyişine katkıda bulununuz...
- Yanıt Olarak İşaretleyen Musa adsız 28 Ocak 2019 Pazartesi 13:53
Tüm Yanıtlar
-
Her iki FormsAuthenticationTicket türündeki nesneye de AdminValue değişkeninin değerini ad olarak vermişsiniz.
Yani iki ticket adı da aynı olmuş. Ticket nesnelerine farklı isimler verin.
Kullanılan parametreler hakkında detaylı bilgi için FormsAuthenticationTicket kullanımı ile ilgili Microsoft dökümanını inceleyebilirsiniz.
Sorunuzun yanıtı bu ise "Yanıt olarak işaretle"yerek siz de forumun işleyişine katkıda bulununuz...
-
Alternatif yöntem olarak iki kullanıcı yetkisini tek FormsAuthenticationTicket içerisinde birleştirebilirsiniz.
UserInformation ui = new UserInformation(); string userData = ""; if (ui.Yetki(2).Yonetim == true | ui.Yetki(2).Okuma == true) userData += "Satis;"; if (ui.Yetki(1).Yonetim == true | ui.Yetki(1).Okuma == true) userData += "Person;"; var ticket = new FormsAuthenticationTicket( 1, AdminValue, DateTime.Now, DateTime.Now.AddMinutes(30), true, userData, FormsAuthentication.FormsCookiePath); string hash = FormsAuthentication.Encrypt(ticket); HttpCookie cokie = new HttpCookie(FormsAuthentication.FormsCookieName, hash); cokie.Expires = ticket.Expiration; Response.Cookies.Add(cokie);
Kullanırken userData içerisindeki rolleri kontrol eder ve yetkileri tespit edebilirsiniz.
Sorunuzun yanıtı bu ise "Yanıt olarak işaretle"yerek siz de forumun işleyişine katkıda bulununuz...
-
-
global.asax dosyasında,
protected void Application_AuthenticateRequest(Object sender, EventArgs e) { if (HttpContext.Current.User != null) { if (HttpContext.Current.User.Identity.IsAuthenticated) { if (HttpContext.Current.User.Identity is FormsIdentity) { FormsIdentity id = (FormsIdentity)HttpContext.Current.User.Identity; FormsAuthenticationTicket ticket = id.Ticket; string userData = ticket.UserData; string[] roles = userData.Split(';'); HttpContext.Current.User = new GenericPrincipal(id, roles); } } } }
kullanabilirsiniz.
NOT: web.config dosyanızı, form yetkilendirme kurallarına göre yapılandırdığınızı varsayıyorum.
Sorunuzun yanıtı bu ise "Yanıt olarak işaretle"yerek siz de forumun işleyişine katkıda bulununuz...
- Yanıt Olarak İşaretleyen Musa adsız 28 Ocak 2019 Pazartesi 13:53
-