Asked by:
How to return json result for login in 3 cases ?

Question
-
problem
problem
How to return json result for login in 3 cases ?
i make web API by dot net core and i need to make login function generate json as following ?
Expected Result Body { "username": "", "password":"", "browser_info" : {TBD} "client_ip" : "" } Response if login succeeded: { "request_status" : { "status" : "succeeded", "code": 0, "message" : "login succeeded!" }, access_token : "", user_data: { UserFullName, LoginTime, Admin }, branches: [ { BranchCode, BranchName } ] } if login failed: { request_status : { "status" : "failed", "code": 1, "message" : "login failed, incorrect username or password" } } if password expired: { request_status : { "status" : "failed", "code": 2, "message" : "password has expired!" } } ---------------------------------- Status Codes: 0 succeeded 1 incorrect username or password 2 password expired
my code as below by using csharp and i using asp.net core 2.2 web api
public string PostUserLogin(string UserId, string Password) { DataAccess.CurrentDataBase = DataAccess.Credentials.SecurityDB; //================ if (!string.IsNullOrEmpty(UserId)) { DbParameter[] parameters = UserManager.CreateLoginParameters(UserId,Password); //:: Check For PassWord Validation days DataTable dt = DataAccess.ExecuteDataTable(SqlFactory.Queries.Userss_ValidatePasswordDays(), parameters); DataRow PassWordDays; if (dt.Rows.Count > 0) PassWordDays = dt.Rows[0]; else PassWordDays = null; if (PassWordDays != null) { if (!string.IsNullOrEmpty(PassWordDays["PasswordDate"].ToString()) && !string.IsNullOrEmpty(PassWordDays["PWDays"].ToString())) { if (Convert.ToInt32(PassWordDays["PWDays"].ToString()) > 0) { if (Convert.ToDateTime(PassWordDays["PasswordDate"].ToString()).AddDays(Convert.ToDouble(PassWordDays["PWDays"].ToString())).ToOADate() < DateTime.Now.ToOADate()) { } } } } //============== bool IsUserValid = UserManager.IsValidUser(UserId, Password); if (IsUserValid) { DataAccess.Credentials.SystemUserID = UserId; DataTable dtGetBranches = DataAccess.ExecuteDataTable( SqlFactory.Queries.Users_GetBranches( DataAccess.Credentials.SystemUserID)); } else { string.Format("Invalid User Name {0} Or Password {1}",UserId,Password); } } } and i user services public class AuthunticationService:IAuthunticationsService { what i write here } public interface IAuthunticationsService { //Post User Login }
- Moved by CoolDadTx Tuesday, September 3, 2019 2:28 PM ASP.NET related
Thursday, August 29, 2019 4:41 AM
All replies
-
Hi engahmedbarbary,
Thank you for posting here.
Since this thread is related to asp.net core, I suggest that you could post it in the following forum.
https://forums.asp.net/1255.aspx/1?ASP+NET+Core
The Visual C# forum discusses and asks questions about the C# programming language, IDE, libraries, samples, and tools.
Best Regards,
Jack
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.Monday, September 2, 2019 9:13 AM -
Hi engahmedbarbary,
Asp.Net core don't need services to be defined with interfaces as WCF. You can create a simple API controller and return custom JSON result, if you want to expose a RESTful API.
.Net core also support dynamic data type, so you can change your response as you want.
public class AuthunticationController:ControllerBase { public IActionResult Authenticate(string username, string password) { //your code for validation //case 1 return OK(new {status:0, message:"your message"}); //case 2 return OK(new {status:1, message:"Your message", data:yourData}); } }
Avik Das
Monday, September 2, 2019 10:41 AM