c# - Setting a post-authentication View for Windows Authentication -
i have mvc4 intranet application (using default template). using windows authentication login system, however, want able capture details user first time register site.
use cases
- first time user authenticates using ad login (currently working). presented 'enter details' view.
- second time user authenticates using ad login. taken straight home screen.
cheers, dave
create custom authorizeattribute this:
public class myauthorizeattribute : authorizeattribute { private unitofwork _unitofwork = new unitofwork(); protected override bool authorizecore(httpcontextbase httpcontext) { var isauthorized = false; var username = httpcontext.user.identity.name; // code find user in database... var user = _unitofwork.userrepository.find(username); if(user != null) { // check if there details user in database if(user.hasdetails) { isauthorized = true; } } return isauthorized; } public override void onauthorization(authorizationcontext filtercontext) { if (filtercontext == null) { throw new argumentnullexception("filtercontext"); } if (!authorizecore(filtercontext.httpcontext)) { // if not authorized, redirect details action // of account controller... var action = filtercontext.routedata.values["action"]; if(filtercontext.controller accountcontroller && action.equals("details")) { // nothing } else { filtercontext.result = new redirecttorouteresult( new system.web.routing.routevaluedictionary { {"controller", "account"}, {"action", "details"} } ); } } } }
then, can use in controllers this:
[myauthorize] public class homecontroller : controller { }
or, can register global action filter in global.asax file this:
public static void registerglobalfilters(globalfiltercollection filters) { filters.add(new myauthorizeattribute()); }
Comments
Post a Comment