c# - How to return correct error message to User when log in is restricted -
i new asp.net web development , stuck in handling exceptions when log in restricted. question: have owner log in , tenant log in. owner can restrict log in tenant. when log in restricted want show tenant credential correct log in restricted.
below have tried till now.
controller
[httppost] [validateantiforgerytoken] public actionresult loginsubmit(loginmv userdata) { if (userdata != null) { usermv authenticateduser = authenticatetenant(userdata.username, userdata.password); if (authenticateduser == null) { modelstate.addmodelerror("invalid credentials"); } else { //do login } } return view("login", userdata); }
user data api
private static usermv authenticatetenant(string username, string password) { res response = mdlapi.authenticatetenant(username, password); try{ response.validate(username); } usermv result = new usermv() { displayname = response.objtenant.lastname + ", " + response.objtenant.firstname }; return result; }
fyi
api response response.resultcode = restricted
.
validate
public static void validate(this res response, string username) { if (response.resultcode != enumerations.authresultcode.successful) { string additionaldetails = ""; if (response.resultcode == enumerations.authresultcode.restricted) { additionaldetails = "login restricted."; } throw new validationexception(additionaldetails); } }
so in above method additionaldetails
set "login restrcited" , validationexception called. also, since exception raised usermv object not created in authenticatetenant function
.
validationexception
public class validationexception : exception { public validationexception(string message) : base (message) { } }
after of sequence of code returns controller. usermv null error message gets displayed "invalid credentials". have missed something.
i getting exception in validationexception class. need else exception displayed user. beginner pardon coding standards. please guide me else need display 'additional detail` stuff tenant.
view file:
@using (html.beginform("loginsubmit", "security", formmethod.post, new { id = "simlogin" })) { @html.antiforgerytoken() <div class="form-horizontal-width-inherit"> @html.hiddenfor(model => model.returnurl) @html.validationsummary(true, "", new { @class = "text-danger" }) <div class="form-group"> @html.validationmessagefor(model => model.username, "", new { @class = "text-danger" }) @html.labelfor(model => model.username, htmlattributes: new { @class = "control-label ignorespacing col-md-2" }) <div class="col-md-10"> @html.editorfor(model => model.username, new { @class = "form-control" } ) </div> </div> <div class="form-group"> @html.labelfor(model => model.password, htmlattributes: new { @class = "control-label ignorespacing col-md-2" }) <div class="col-md-10"> @html.passwordfor(model => model.password, new { @class = "form-control" } ) </div> </div> <div class="form-group"> <div class="col-md-offset-2 col-md-10"> <input type="submit" value="login" class="btn btn-default" /> </div> </div> </div> }
you seem relying on authenticatetenant()
check credentials , return
authentication result.
but, internally you're calling validate()
in turn throwing exception. exception changing control flow of code. currently, code seems catch either error filter have or it's propagated down iis , generic http 500 being displayed.
i suggest not throw exception part of authenticatetenant()
method rather return null
(or false
, if you're willing change method signature).
once you're in control of flow, can rely on returned result rightfully call modelstate.addmodelerror()
proper message.
Comments
Post a Comment