asp.net mvc - Determine security on shared layout -
in asp mvc 3 website, need way determine user security on shared layout page. layout page houses navbar needs display drop down items based on user's security level.
originally had thought make ajax call , populate viewbag item, use determine show/not show. however, won't work unless want put same method in every controller/method.
given set (navbar located on shared layout), best method determining items show user navigates across different controllers/methods?
you go 2 ways this.
you check in view:
@if (user.identity.isauthenticated){ // show logged in view } else{ // show logged out view }
or build viewmodel , populate shared action.
example:
viewmodel
public class vm { public string text{get; set;} }
shared action on shared controller:
public class sharedcontroller{ public partialviewresult getmenu(){ vm newvm = new vm(text = "not logged in"); if (user.identity.isauthenticated){ newvm.text = "logged in"; } return partialview("shared", newvm); } }
a partialview render action:
@model vm <p> @model.text </p>
and lastly in view:
@{ html.renderaction("shared", "shared"); }
Comments
Post a Comment