asp.net mvc - Getting userId of logged in user returns null -


i designing asp.net mvc4 application , need current user id i'll save in table in database owner id group.

this code using:

public actionresult creategroup(string groupname, string description) {        if (request.isauthenticated)        {            group group = new group();            random random = new random();            int groupid = random.next(1, 2147483647);             using (codeshareentities conn = new codeshareentities())            {                try                {                         int ownerid = (int) membership.getuser(system.web.httpcontext.current.user.identity.name).provideruserkey;                          group.groupid = groupid;                         group.groupname = groupname;                         group.description = description;                         group.ownerid = ownerid;                          conn.group.add(group);                         conn.savechanges();                     }                     catch (exception e)                     {                         modelstate.addmodelerror("", e.message);                     }                 }                  return view("thisgroup");           }           else            {           return view("../shared/notloggedin");   } 

i getting nullreferenceexception on line:

int ownerid = (int) membership.getuser(system.web.httpcontext.current.user.identity.name).provideruserkey; 

although username returning correctly. can problem? still new web applications , can't figure out!

thanks

unless using custom membership provider, believe providersuserkey guid.

(guid) membership.getuser(system.web.httpcontext.current.user.identity.name).provideruserkey; 

Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -