i have problem about posting system default values.
specifically:
I have model where one field is userName. model looks like
public class MyTable {
[key]
public int ID { get; set; }
public string Firstname{get;set;}
public string LastName{get;set;}
public string JobName{get;set;}
publi string UserName{get;set;}
}
My problem is, when user is logged in and posting data, want to in UserName default value will be current logged user name. what is best practice how to take default values from controller or from razor page.
public ActionResult Create([Bind(Include = "ID,Firstname,LastName,JobName,UserName")]
MyTable myTable) { if (ModelState.IsValid) { db.MyTable.Add(MyTable); db.SaveChanges(); return RedirectToAction("Index"); }
In this case i don't know how to set default values in model, because here post is on from model and not from custom fields. How to split by fields or how to set default value in binding ?
post back data from a browser, should only be data the user is allowed to modify. A browser user should not be allowed to modify the logged in name. You should remove it from the bind, and set the value in the controller. also as the key is an int, and thus
easy to guess a valid value, you should encrypt it before sending to the client.
your current implementation is very open to hackers.
Not 100% crystal clear. This is a default value but the user should be able to edit that and you have a postback or it is necessarily the "current user" ?
User.Identity.Name and then I expect you are able to retrieve information from this user. Or until know authentication is not yet configured for your site ?
Edit: you are trying to let the authenticated user to edit its own profile ?
my problem is before save changes, i want to assign custom values by properties. I think from controller i can get username ad put it in properties.
If you are using
standard authentication, the username is cached in an authentication cookie. The following code fetches the username (of the current logged in user_ and assigns the username to a model property.
myTable.UserName = User.Identity.Name
If you are building a custom account management system, then the community needs a lot more information. You'll need to share enough code to reproduce this issue and explain how your custom code works.
Member
12 Points
54 Posts
Problem with system default values
Nov 15, 2019 10:15 AM|aprangulashvili|LINK
Hi all
i have problem about posting system default values.
specifically:
I have model where one field is userName. model looks like
public class MyTable {
[key]
public int ID { get; set; }
public string Firstname{get;set;}
public string LastName{get;set;}
public string JobName{get;set;}
publi string UserName{get;set;}
}
My problem is, when user is logged in and posting data, want to in UserName default value will be current logged user name. what is best practice how to take default values from controller or from razor page.
public ActionResult Create([Bind(Include = "ID,Firstname,LastName,JobName,UserName")] MyTable myTable)
{
if (ModelState.IsValid)
{
db.MyTable.Add(MyTable);
db.SaveChanges();
return RedirectToAction("Index");
}
In this case i don't know how to set default values in model, because here post is on from model and not from custom fields. How to split by fields or how to set default value in binding ?
All-Star
58484 Points
15810 Posts
Re: Problem with system default values
Nov 15, 2019 03:44 PM|bruce (sqlwork.com)|LINK
post back data from a browser, should only be data the user is allowed to modify. A browser user should not be allowed to modify the logged in name. You should remove it from the bind, and set the value in the controller. also as the key is an int, and thus easy to guess a valid value, you should encrypt it before sending to the client.
your current implementation is very open to hackers.
All-Star
48740 Points
18193 Posts
Re: Problem with system default values
Nov 15, 2019 03:51 PM|PatriceSc|LINK
Hi,
Not 100% crystal clear. This is a default value but the user should be able to edit that and you have a postback or it is necessarily the "current user" ?
User.Identity.Name and then I expect you are able to retrieve information from this user. Or until know authentication is not yet configured for your site ?
Edit: you are trying to let the authenticated user to edit its own profile ?
Member
12 Points
54 Posts
Re: Problem with system default values
Nov 16, 2019 11:33 AM|aprangulashvili|LINK
Hi
I have problem in entity framework action. how to insert custom value. Again
public ActionResult Create([Bind(Include = "ID,Firstname,LastName,JobName,UserName")] MyTable myTable)
{
if (ModelState.IsValid)
{
db.MyTable.Add(MyTable);
db.SaveChanges();
return RedirectToAction("Index");
}
my problem is before save changes, i want to assign custom values by properties. I think from controller i can get username ad put it in properties.
All-Star
53751 Points
24069 Posts
Re: Problem with system default values
Nov 16, 2019 12:47 PM|mgebhard|LINK
If you are using standard authentication, the username is cached in an authentication cookie. The following code fetches the username (of the current logged in user_ and assigns the username to a model property.
If you are building a custom account management system, then the community needs a lot more information. You'll need to share enough code to reproduce this issue and explain how your custom code works.
Member
12 Points
54 Posts
Re: Problem with system default values
Nov 16, 2019 02:30 PM|aprangulashvili|LINK
Thanks