Separating roles authorization and database

Something really bothers me about the way authorization tends to be done with roles in ASP.NET MVC.

The way it is normally done is that you have a Users table and a Roles table. A User can have many Roles.

Then you decorate your controllers/actions with an authorize attribute saying which roles you want to allow access to the controller/action:

[Authorize(Roles = "Admin")]
public ActionResult AdminOnlyAction()
{
}

The problem with this is that your code is reliant on data within the database.

Obviously you could, at application start, seed your database with the roles needed if they’re not present but there’s still a fragile dependency on the data in the database (e.g. it could be deleted, renamed, etc. etc.).

Having an enum or similar with the Role levels in instead of storing them in the database seems reasonable but still leaves me with a sour feeling.

Is there an elegant alternative which I’ve not hit upon?

1

Obviously you could, at application start, seed your database with the roles needed ...

I would suggest that you should be doing this at installation time, not application start-up. A “user-level” connection probably shouldn’t have permission to make that sort of change anyway.

... there's still a fragile dependency on the data in the database ... 

What’s “fragile” about it? It’s no different to [pretty much] every other application ever written that uses a database. If the table structures aren’t set up the way that the application expects, it crashes and burns.

Surely the important issue here is that the relationships between Users and Roles live outside of your code.
The access decorators that you add to your code are static. You mark a method as “Administrators only” and that’s that.

The association of a User with a Role is dynamic. Different people will hold different roles over time; that’s all captured in the database and your code doesn’t change.

First, if you are ensuring they are created — and I’ll disagree with Phill W. here and note that having an “ensure roles” task on startup is a great approach — then they aren’t fragile.

If end users are doing something like manually editing your database then that gets into warranty voiding tampering so things not working would be expected in my book.

Finally, you can build custom attributes that allow you to do more than hardcode Role=Admin. One app we did which was tied to windows had a pretty nifty custom mapping scheme where it had internal groups mapped to external NT groups so the admins weren’t stuck with specifically named groups. This app would also create it’s own NT groups if you’d let it.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật