c# - Cannot authenticate in Web API service called from MVC website -
i have asp.net mvc website using angular front end, needs communicate rest web api service retrieve data. have authentication logic against custom provider in mvc project , works fine using adlds. calls rest web api have no authentication data passed , can't work out how pass user authenticated mvc, on rest web api.
here example call web api.
public void approvemodel(int modelversionid, string comments) { var request = new restrequest("api/modelversion", method.post) { requestformat = dataformat.json }; request.addbody(new[] { new modelaction { modelactiontype = modelactionconstants.modelactionapprovemodel, actionparameters = new dictionary<string, string> { { modelactionconstants.modelactionparametermodelversionid, modelversionid.tostring(cultureinfo.invariantculture) }, {modelactionconstants.modelactionparametercomments, comments} } } }); request.usedefaultcredentials = true; var response = _client.execute(request); if (response.statuscode != httpstatuscode.ok) throw new serverexception(response.content); }
my web api controller method (abbreviated)
[validateaccess(constants.dummy, constants.secure_environment)] public httpresponsemessage post(modelaction[] actions) { ... }
and custom validate access attribute uses thinktecture
public class validateaccess : claimsauthorizeattribute { private static readonly ilog log = logmanager.getlogger(typeof(validateaccess)); private readonly string _resource; private readonly string _claimsaction; public validateaccess(string claimsaction, string resource) { _claimsaction = claimsaction; _resource = resource; xmlconfigurator.configure(); } protected override bool isauthorized(httpactioncontext actioncontext) { if (actioncontext == null) throw new argumentnullexception("actioncontext"); if (!httpcontext.current.user.identity.isauthenticated) { log.infoformat("user {0} not authenticated - not authorizing further. redirecting error page.", httpcontext.current.user.identity.name); return false; } // specified users or roles when use our attribute return checkaccess(actioncontext); } protected override bool checkaccess(httpactioncontext actioncontext) { if (_claimsaction == string.empty && _resource == string.empty) { //user in landing page return true; } return claimsauthorization.checkaccess(_claimsaction, _resource); } }
my problems
i not familiar web api.
isauthorized(httpactioncontext actioncontext)
right method override enforce access policy on api calls?why getting null user identity?
Comments
Post a Comment