asp.net mvc 4 - How to create a custom attributes that derive from AuthorizeAttribute? -
after user has create account, he/she needs create profile he/she submits information used letter. have 2 such controllers need information user's profile.
i'm trying create attribute check whether user has created profile before letting him access action methods. if not, user should redirected profile page.
public class notauthorizedwithoutprofileattribute : authorizeattribute { protected override bool authorizecore(httpcontextbase httpcontext) { var username = httpcontext.profile.username; var repository = new owrepository(); var userinfo = repository.getuserinfo(username); if (userinfo == null) return false; else return true; } }
the problem having concerning username, i.e. following line
var username = httpcontext.profile.username;
why giving null? yet how i've getting logged user's name.
i don't know profile configuration it's safest use httpcontextbase.user.identity.name
user name.
also consider changing class name shorter meaningful requireprofileattribute
.
Comments
Post a Comment