64

how can i check if a user is logged in in user control with asp.net mvc

usually on a view page i use this

<% if (User.Identity.IsAuthenticated) {%>
  //Do something
<% } %>

but i can't get this done on a user control

4 Answers 4

77

Does this work?

<%= Page.User.Identity.IsAuthenticated %>
Sign up to request clarification or add additional context in comments.

1 Comment

Or try <%= Context.User.Identity.IsAuthenticated %>
74

Nothing new to add to Griegs answer, but I would normally do

@Request.IsAuthenticated

3 Comments

Looking at the reference sources for HttpRequestWrapper and then HttpRequest the IsAuthenticated property is implemented with User.Identity.IsAuthenticated, among other things. return(_context.User != null && _context.User.Identity != null && _context.User.Identity.IsAuthenticated);
So what's the difference? Or are these two identical?
Pretty much. Mine is shorter. :-)
9

You could decorate the Method with the Authorize attribute. This requires that the User calling the Method being authenticated.

Comments

0

Well I use VB

If User.Identity.Name = "" Then
   Response.Redirect("~/Login.aspx")
Else
   ........continue...........
End If

1 Comment

As the original question did not have access to User, how would this make any difference "in a user control". Your example is in a controller and not a user-control. -1

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.