开发者

How to work with Identity in ASP.NET MVC

How to work with Identity in asp.net mvc. How does Identity work with membership provider, and ro开发者_运维问答le provider? Thank you!


There really are two problems you are looking at:

  • Authentication, easily solved by the built - in membership provider or open auth or LDAP or whatever. Standard ASP.NET backings are in effect.
  • Authorization, the interesting part. Depending on what is going in it might not matter or it could be insanely finely grained. Default would be to ride the rails of the ASP.NET RoleProviders.

At the end of the day, authentication ins ASP.NET MVC2 isn't much different from authentication ASP.NET. Standard fares apply.


I don't think there's anything special. It should "just work". The default template in MVC in fcat creates a bunch of boiler-plate code for you.


It works pretty much the same as in ASP.NET WebForms, but you control access to different parts of your site by decorating actions or controllers with attributes.

Example:

public class HomeController
{
      // Does not require any authentication
      public ActionResult Index(int id)
      {
            return View();
      }


     // Requires login, and that the logged in user is in the "Users"-Role
     [Authorize(Roles="Users")]
     public ActionResult SemiSecret(int id)
     {
            return View();
     }

     // Same as above, but requires user to be in "Admin" Role
     [Authorize(Roles="Admin")]
     public ActionResult TopSecret(int id)
     {
           return View();
     }
}

on a controller:

// All actions in this controller requires users to log in and be in "Admin" role
[Authorize(Roles="Admin")]
public class AdminController
{
      // Controller code goes here ...
}

You can also limit this on a userlevel by using [Authorize(Users="UserName")]

Hope this helps!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜