के लिए भूमिका आधारित प्राधिकरण कैसे करें मैं एक सुरक्षित एएसपीनेट वेब एपीआई बनाने की कोशिश कर रहा हूं। कि मैं नीचे दिए गए लिंकएएसपीनेट एमवीसी 4 वेब एपीआई
का पालन किया है तो अब हर api अनुरोध एक टोकन जो मैं नीचे के रूप में उदाहरण के लिए
public class TestController : Controller
{
public string GetProducts()
{
Uri myUri = new Uri("http://localhost:420420/api/products");
WebRequest myWebRequest = WebRequest.Create(myUri);
myWebRequest.Method = "GET";
myWebRequest.ContentType = "application/json";
myWebRequest.Headers.Add("Authorization-Token", RSAClass.accessToken);
using (WebResponse response = myWebRequest.GetResponse())
{
using (var responseStream = response.GetResponseStream())
{
var reader = new StreamReader(responseStream);
return reader.ReadToEnd();
}
}
}
}
तो मैं अब कर रहा हूँ अनुरोध हेडर में आपूर्ति कर रहा हूँ की जरूरत है प्रत्येक एपीआई अनुरोध करने में सक्षम, शीर्षलेख में टोकन की जांच करें। लेकिन मैं प्रमाणीकरण कैसे पूरा कर सकता हूं, मेरा मतलब है कि मैं इस टोकन को उसी नियंत्रक में कुछ कार्रवाइयों तक नहीं पहुंचने की अनुमति कैसे दे सकता हूं। मुझे बस एक विचार चाहिए। आशा है कि मैंने काफी अच्छी तरह समझाया है।
संपादित करें:
public class TestController : Controller
{
public string GetProducts()
{
Uri myUri = new Uri("http://localhost:420420/api/products");
WebRequest myWebRequest = WebRequest.Create(myUri);
myWebRequest.Method = "GET";
myWebRequest.ContentType = "application/json";
myWebRequest.Headers.Add("Authorization-Token", RSAClass.accessToken);
**using (WebResponse response = myWebRequest.GetResponse())
{
using (var responseStream = response.GetResponseStream())
{
var reader = new StreamReader(responseStream);
return reader.ReadToEnd();
}
}**
}
मैं, "API" नियंत्रक के लिए एक अनुरोध बनाने रहा नियंत्रक ऊपर अंदर, webrequest का उपयोग कर (मैं इसे बाद में HttpClient में बदल जाएगा)। के बीच ** ** ऊपर मैं myWebRequest.GetResponse() के लिए नहीं मिला 404 पृष्ठ हो रही है
नीचे delegating हैंडलर में अब मेरी एपीआई नियंत्रक
public class ProductsController : ApiController
{
TestModelContainer testModel = new TestModelContainer();
[Authorize(Roles="Users")]
public IEnumerable<Products> GetProducts()
{
IEnumerable<Products> products = (from prods in testModel.Products
select prods);
return products;
}
}
}
है मैं कोड में कोड
public class TokenValidationHandler : DelegatingHandler
{
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request,
CancellationToken cancellationToken)
{
TestModelContainer testModel = new TestModelContainer();
var token = "";
try
{
if (request.Headers.Contains("Authorization-Token"))
{
token = request.Headers.GetValues("Authorization-Token").FirstOrDefault();
if (String.IsNullOrEmpty(token))
{
return Task<HttpResponseMessage>.Factory.StartNew(() =>
{
return new HttpResponseMessage(HttpStatusCode.BadRequest)
{
Content = new StringContent("Missing Authorization-Token")
};
});
}
}
else
{
return Task<HttpResponseMessage>.Factory.StartNew(() =>
{
return new HttpResponseMessage(HttpStatusCode.BadRequest)
{
Content = new StringContent("You need to include Authorization-Token " +
"header in your request")
};
});
}
var decryptedToken = RSAClass.Decrypt(token);
var foundUser = (from user in testModel.Users
where user.Name == decryptedToken
select user).Any();
if (!foundUser)
return Task<HttpResponseMessage>.Factory.StartNew(() =>
{
return new HttpResponseMessage(HttpStatusCode.Forbidden)
{
Content = new StringContent("Unauthorized User")
};
});
var identity = new GenericIdentity(decryptedToken);
string[] roles = new string[] { "Users", "Testers" };
var principal = new GenericPrincipal(identity, roles);
Thread.CurrentPrincipal = principal;
}
catch (Exception ex)
{
return Task<HttpResponseMessage>.Factory.StartNew(() =>
{
return new HttpResponseMessage(HttpStatusCode.InternalServerError)
{
Content = new StringContent("Error encountered while attempting to process authorization token")
};
});
}
return base.SendAsync(request, cancellationToken);
}
404 त्रुटि वृद्धि नहीं करता है अगर मैं एपीआई नियंत्रक से अधिकृत विशेषता निकालने के लिए, और फिर मैं इसे का उपयोग करने में सक्षम हूँ के बाद।
अद्यतन (मेरा मानना है कि समाधान भी):
यह कैसे समस्या हल हो जाती गया
मैं TestController विधि बदल गया है नीचे के रूप में सुझाव दिया डैरिन दिमित्रोव
public class TestsController : Controller
{
public ActionResult GetProducts()
{
var productsUrl = Url.RouteUrl("DefaultApi", new { httproute = "", controller = "products" }, "http");
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Add("Authorization-Token", RSAClass.accessToken);
var products = client
.GetAsync(productsUrl)
.Result;
if (products.StatusCode == HttpStatusCode.Unauthorized)
{
return Content("Sorry you are not authorized to perform this operation");
}
var prods = products.Content
.ReadAsAsync<IEnumerable<Products>>()
.Result;
return Json(prods, JsonRequestBehavior.AllowGet);
}
}
कर रहा है मुद्दा यह था कि मुझे एपीआई को कॉल करने का तरीका नहीं पता था, धन्यवाद डरिन उनके महान समर्थन के लिए (वह बहुत जल्दी था)।
धन्यवाद
मैं पहले से ही TokenValidation पंजीकृत किया है global.asax.cs में, लेकिन मेरा सवाल मैं एक प्रमाणीकृत टोकन कैसे बना सकता हूं, उसी नियंत्रक में कुछ अन्य कार्रवाइयों तक नहीं पहुंच सकता ...? मेरा मतलब है कि मैं "एपीआई नियंत्रक" – CrazyNooB
में जेनरेट किए गए टोकन के साथ [प्राधिकरण (भूमिकाएं = "व्यवस्थापक")] कैसे पूरा करूं, आपको एक अलग 'टोकन वैलिडेशन हैंडलर' लिखना होगा। इस आलेख में प्रस्तुत किया गया केवल टोकन के अंदर उपयोगकर्ता नाम स्टोर करता है, बिल्कुल कोई भूमिका नहीं। आप निम्नलिखित उत्तर की जांच कर सकते हैं जिसमें मैंने बुनियादी प्रमाणीकरण योजना का उपयोग करके एक कस्टम प्रतिनिधि हैंडलर और अंतर्निहित सदस्यता और भूमिका प्रदाताओं पर भरोसा किया है: http://stackoverflow.com/a/11536349/29407 –
बिल्कुल वही है जो मैं पूछ रहा था । क्या आप कोड का मामूली टुकड़ा दे सकते हैं कि टोकन के साथ भूमिका आधारित प्राधिकरण कैसे करें ..? – CrazyNooB