2008-10-26 13 views
10

मैं एमवीसी खाता नियंत्रक को देख रहा हूं, और ऐसा लगता है कि यह एएसपी.नेट वेबफॉर्म से है। क्या इसका उपयोग करने के बारे में कोई अच्छी पृष्ठभूमि जानकारी है?एएसपी.नेट एमवीसी खाता नियंत्रक उपयोग दिशानिर्देश?

क्या आप इसे किसी उपयोगकर्ता डेटाबेस तालिका में मैप कर सकते हैं या अपने उपयोगकर्ता प्रबंधन को रोल करना बेहतर है?

एमवीसी में आप इसका उपयोग कैसे कर सकते हैं ताकि उपयोगकर्ता लॉग इन किए गए पृष्ठों को प्रतिबंधित कर सकें? क्या आपको यह सब खुद को रोल करना है?

एएसपी.NET सदस्यता को समझने में वेब पर कौन से संसाधन मदद कर सकते हैं?

उत्तर

18

मैं MVC खाता नियंत्रक पर देख रहा हूँ .... यह asp.net से हो रहा है?

स्कॉट गुथरी ASP.NET MVC Preview 4 के बारे में अपने ब्लॉग एंट्री में यह काफी अच्छा बताते हैं। वह मूल रूप से कहता है कि एमवीसी नमूना से खाता नियंत्रक एएसपी.NET सदस्यता प्रदाता का उपयोग करता है, ताकि आप इनमें से किसी का भी उपयोग कर सकें। (मुझे लगता है कि आप इंटरनेट पर एएसपी.NET सदस्यता प्रदाताओं के बारे में अधिक जानकारी प्राप्त कर सकते हैं।) यदि आप उनमें से किसी एक को लागू/उपयोग नहीं करना चाहते हैं, तो अपने उपयोगकर्ता प्रबंधन का उपयोग करने के लिए एप्लिकेशन को संशोधित करना शायद सबसे अच्छा विकल्प होगा।

से कैसे रोक सकता क्या पृष्ठ उपयोगकर्ताओं में लॉग इन देख सकते हैं आप को MVC में इसका इस्तेमाल कर सकता हूँ? क्या आपको अपने सभी को रोल करना होगा?

आप नियंत्रक वर्ग या क्रिया विधि में Authorize विशेषता जोड़ सकते हैं। (उपरोक्त के रूप में source।)

// Only logged in users can access this controller. 
[Authorize] 
public class SomeController : Controller 
{ 
    #region Not really important for this example. :] 
    // Maybe rather use a BLL service here instead of the repository from the DAL, but this example is already more verbose than required. 
    private IStuffRepository stuffRepository; 

    public SomeController(IStuffRepository stuffRepository) 
    { 
     if (null == stuffRepository) 
     { 
      throw new ArgumentNullException("stuffRepository"); 
     } 

     this.stuffRepository = stuffRepository; 
    } 
    #endregion 

    // The authorize attribute is inherited - only logged in users can use the index action. 
    public ActionResult Index() 
    { 
     return View(); 
    } 

    // Moderators can flag stuff. 
    [Authorize(Roles="Moderator")] 
    public ActionResult Flag(int id) 
    { 
     this.stuffRepository.Flag(id); 
     return RedirectToAction("Index"); 
    } 

    // Admins ans SysOps can delete stuff. 
    [Authorize(Roles="Admin,SysOp")] 
    public ActionResult Delete(int id) 
    { 
     this.stuffRepository.Delete(id); 
     return RedirectToAction("Index"); 
    } 

    // Only joed can change the objects stuff. ;) 
    // (This is probably bullshit, of course, but I could not make any better example. I blame the fact it is late at night. :)) 
    [Authorize(Users="COMPANY\\joed")] 
    public ActionResult ChangeId(int oldId, int newId) 
    { 
     this.stuffRepository.ChangeId(oldId, newId); 
     return RedirectToAction("Index"); 
    } 
} 
+0

अच्छा उदाहरण, डी और महान टिप्पणियां! +1 – Matt

संबंधित मुद्दे