0

Context.User.Identity.Name is always empty, currently I am using WebForms:

HttpCookie cookie = new HttpCookie("jamal");
cookie.Value = "bilal";
HttpContext.Current.Response.Cookies.Add(cookie);

Here I want username:

public override Task OnConnected()
{
    AddUser(Context.User.Identity.Name, this.Context.ConnectionId);
}

private static void AddUser(String username, String connectionId)
{
    ISet<String> connections;

    if (users.TryGetValue(username, out connections) == false)
    {
        connections = users[username] = new HashSet<String>();
    }

    connections.Add(connectionId);
}
Allan Pereira
  • 2,572
  • 4
  • 21
  • 28
bilal
  • 648
  • 8
  • 26

3 Answers3

1

This happens because you haven't required authentication for this action.
Require that the user is authorized for the action by decorating it with the Authorize attributeclass - which will initiate the authentication negotiation. I never tried with webform but it should work if you add in the web config this rule:

<authentication mode="Forms" />
Tinwor
  • 7,765
  • 6
  • 35
  • 56
0

Try to call this on Page_Load event, you are probably calling it too soon.

HoTTab1CH
  • 199
  • 3
  • 20
0

The way you are creating your cookie, it is not an authentication cookie. Asp.net handles the authentication process for you if you create a new asp.net web forms project. But if you want to create your own authentication cookie, these are the steps that you have to follow.

The FormsAuthenticationModule from asp.net authenticates the user by checking the authentication ticket. Your cookie has only one name. You have to create the ticket. Also check this link.

Community
  • 1
  • 1
Turay Melo
  • 114
  • 5