Kassem
Kassem

Reputation: 8266

Input string was not in a correct format

I keep getting this exception (in the title) whenever I try to get the user id from the session (FormsAuthentication). Here's the code of the method:

    public User GetUserFromSession()
    {
        int userId = int.Parse(_httpContext.User.Identity.Name); //this line throws the exception
        IsNotNull(userId, "session user id");
        var user = _userService.GetUserById(userId);
        return user;
    }

When I first add the ID to the session, it is being added as a string. I tried using the Convert.Int32 and Convert.Int16 but I still got the same exception. How can I stop that from happening?

UPDATE:

Ok I debugged the project and checked the value of _httpContext.User.Identity.Name and it was actually an empty string! I do not understand why that is happening though... Here's how I login the user:

var authTicket = new FormsAuthenticationTicket(
    1,
    user.Id.ToString(), //user id
    DateTime.Now,
    DateTime.Now.AddDays(30), // expiry
    dto.RememberMe, //true to remember
    "", //roles 
    "/"
    );

//encrypt the ticket and add it to a cookie
var cookie = new HttpCookie(FormsAuthentication.FormsCookieName,
                            FormsAuthentication.Encrypt(authTicket));
Response.Cookies.Add(cookie);

Shouldn't that do the job? Or am I missing something here...

UPDATE 2:

Check out this screenshot: http://i52.tinypic.com/2cnaw00.jpg As you can see, the CurrentNotification property says there's an exception. Could that be real source of the problem?

P.S: The Helper class takes an HttpContext instance in its constructor, this is how I'm passing the current context: x.For<HttpContext>().Use(HttpContext.Current); (using StructureMap).

Upvotes: 1

Views: 1461

Answers (6)

Steve Hobbs
Steve Hobbs

Reputation: 3326

Trying using FormsAuthentication.SetAuthCookie() (http://msdn.microsoft.com/en-us/library/twk5762b.aspx) instead to see if that works, instead of creating the cookie yourself.

You mentioned this now works in your comment above, but I'm afraid I'm unsure why without seeing more context in your code sample. It could be something to do with the parameters you're passing to the FormsAuthenticationTicket constructor (check the sample at http://msdn.microsoft.com/en-us/library/system.web.security.formsauthenticationticket.aspx - perhaps the forms cookie path parameter is wrong in your case?). Try using FormsAuthentication.FormsCookiePath as the last parameter, instead of "/" just to eliminate that possibility.

Upvotes: 1

Philip Smith
Philip Smith

Reputation: 2801

The FormatException is raised by int.Parse when the string passed to it does contain a valid number.

Upvotes: 0

Craig Suchanec
Craig Suchanec

Reputation: 10804

Problem is the string you trying to parse is not a number. See the documentation on the Parse() method.

FormatException: s does not consist solely of an optional negative sign followed by a sequence of digits ranging from 0 to 9.

You might want to use TryParse() instead. That way a failure in parsing will return a false rather than create an exception.

Also is name really a integer or did you intend to use some other field that would contain an integer?

Upvotes: 0

Steve Hobbs
Steve Hobbs

Reputation: 3326

_httpContext.User.Identity.Name is probably not parsable as an integer in this case. You should debug or print out the Identity.Name property to make sure that it can be evaluated as an integer, or use int.TryParse() to evaluate the string to avoid the exception (depending on your requirements).

Upvotes: 0

Bogdan Verbenets
Bogdan Verbenets

Reputation: 26936

Probably because httpContext.User.Identity.Name is a user name, not a user id.

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

You will get this exception if the string you are trying to parse to an integer is not a valid integer. For example if the username of the currently connected user is "john":

int userId = int.Parse("john");

you will get this exception.

Upvotes: 0

Related Questions