2

I have an ASP.NET MVC application. This application has a controller that does not have any attributes on it. This controller has a fairly complex action. Up-to-this point, the action has had [AllowAnonymous] on it. Due to its complexity, I want to share this action with both Authorized and Unauthorized users. In the event of an Authorized user, I'm going to add one basic piece to this complex action.

I tried adding [Authorize] as suggested in this post. However, no matter what, User.Identity.IsAuthenticated is always false. I have my own membership database structure. However, I've tied it into the System.Web.Security stuff. So, when someone logs in, I have:

FormsAuthentication.SetAuthCookie(model.Username, model.RememberMe);

This has worked fine up to this point.

How do I allow both authorized and unauthorized users to access a controller action, while identifying if the user is authorized or not? Thank you!

3
  • "'User.Identity.IsAuthenticated' is always false" - If you're using your own database structure and you've tied it into the System.Web.Security stuff, are you sure this is being set properly? Commented Apr 12, 2013 at 14:48
  • Looks similar to stackoverflow.com/questions/15770413/… Commented Apr 18, 2013 at 16:20
  • Sounds like the question should really be: "Why is User.Identity.IsAuthenticated always returning false?" - and then post your code... Commented Apr 26, 2013 at 10:48

1 Answer 1

1

Try this in your Controller/View:

if(Request.IsAuthenticated) 
{ 
    doAuthenticatedStuff(); 
}
else
{
    doAnonymousStuff();
}

Does that help?

Sign up to request clarification or add additional context in comments.

1 Comment

That should do the trick. Probably only the if without the else if I read the start post correctly, but that's just the details. No authoriza attributes required.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.