如何做asp.net的MVC 4个Web API基于角色的授权 [英] How to do role based authorization for asp.net mvc 4 web api
问题描述
我试图做一个安全的asp.net网页API。对于我按照以下链接
I am trying to make a secure asp.net web api. For that I have followed the below link
<一个href=\"http://$c$cbetter.com/johnvpetersen/2012/04/04/moving-from-action-filters-to-message-handlers/\">MessageHandler令牌
所以,现在每一个API请求需要它,我在请求头下面例如提供一个令牌
So now each and every api request needs a token which I am supplying in the request header as below for example
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请求,为您在标头中的令牌。但我怎么做到的授权,我是说我怎么能不使此令牌在同一controller.I无法访问某些动作只需要一个idea.Hope我解释不够好。
So I am now able to make each and every api request, check for a token in the header. But how do I accomplish authorization, I mean how can I not allow this token not access some actions in the same controller.I just need an idea.Hope I explained well enough.
编辑:
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)。在这两者之间的code **上面我得到的404页没有找到 myWebRequest.GetResponse()的
I am making a request to the "api" controller, inside above controller, using webrequest(I will change it later to HttpClient). In the code between ** ** above I am getting 404 page not found for myWebRequest.GetResponse()
下面是我的API控制器
Below is my api controller
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;
}
}
}
现在在委托处理我有以下的code
Now in the delegating handler I have the following code
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错误犯规上涨,如果我请从API控制器的授权属性,然后我能够访问它。
The 404 error doesnt rise if i remove the Authorize attribute from the api controller, and then I am able to access it.
更新(我相信解决方案太):
这是问题是怎么解决的。
this is how the issue got solved
我已经改变了方法的TestController如下建议达林季米特洛夫
I have changed the TestController method as below suggested by Darin Dimitrov
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);
}
}
问题是我不知道如何对API的调用,这要感谢的达林作为他的大力支持(他的速度非常快太)。
The issue was I didnt know how to make a call to the api, thanks to Darin for his great support(he was very quick too).
感谢
推荐答案
您注册在处理程序的Global.asax
:
GlobalConfiguration
.Configuration
.MessageHandlers
.Add(new TokenValidationHandler());
再装点控制器/需要使用 [授权]
属性的授权行动:
public class MyController : ApiController
{
[Authorize]
public string Get(string id)
{
...
}
}
有关基于角色的授权你可能可以看看下面的例子: http://stackoverflow.com/a/11536349/ 29407
它通过SSL使用基本身份验证,并依靠内置的成员资格和角色提供。
It uses basic authentication over SSL and relies on the built-in membership and role providers.
更新:
据众多评论离开我得到的IM pression,我的答案是不太清楚。让我解释。
According to the numerous comments left I get the impression that my answer was not clear enough. Let me elaborate.
- 创建使用空模板创建新的ASP.NET MVC 4项目
-
定义的模型:
- Create a new ASP.NET MVC 4 project using the Empty Template
Define a model:
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
}
定义一个ApiController:
Define an ApiController:
public class ProductsController : ApiController
{
// GET /api/products => only users having the Users role can call this
[Authorize(Roles = "Users")]
public HttpResponseMessage Get()
{
var products = Enumerable.Range(1, 5).Select(x => new Product
{
Id = x,
Name = "product " + x
});
return Request.CreateResponse(HttpStatusCode.OK, products);
}
// GET /api/products => only users having the Admin role can call this
[Authorize(Roles = "Admin")]
public void Post(Product product)
{
}
}
定义 RSAHelper
:
public class RSAClass
{
private static string _privateKey = "<RSAKeyValue><Modulus>poQS/c9tLkgg84xYZpnUBHP6fy24D6XmzhQ8yCOG317hfUNhRt6Z9N4oTn+QcOTh/DAnul4Q901GrHbPrMB8tl1LtbpKbvGftPhyR7OLQVnWC1Oz10t2tHEo7mqyPyAVuYsq8Q1E3YNTh2V6+PRvMiAWGUHGyyG7fKjt/R9W+RE=</Modulus><Exponent>AQAB</Exponent><P>4G09wYejA4iLakpAcjXbE/zV9tXTNsYqVIWeXF4hzwMmwmin7ru/WQzXu2DdapXXOJIKqrkfzXlcPwCsW5b9rQ==</P><Q>vfEq13Et+cP4eGgsR+crDQH0Mi+G6UW5ACfuDs/zam1o+CE70pLgeWawfqW4jRN30/VHDnTF9DZuotH6zihNdQ==</Q><DP>JoZaHYidERQ1am+IlJJuIwY57H9UHIjz50JwpsZ540FVO/YfLboI5M5xkfbUy2EhatKXBit1LB5zGVWSQL6wmQ==</DP><DQ>Gxk7KX2GN6oT2unR13hNlg9/TWGmd8VwvWr09bwJWFe/sBbduA8oY2mZKJhwGgB7CgxmVNOoIk1Zv3UBuUPauQ==</DQ><InverseQ>ZwJpSUZ09lCfiCF3ILB6F1q+6NC5hFH0O4924X9B4LZ8G4PRuudBMu1Yg0WNROUqVi3zfihKvzHnquHshSL56A==</InverseQ><D>pPQNRDVpeQGm8t1C7VDRwR+LNNV7krTMMbXGiJT5FOoPAmHvSZ9WcEZrM2gXFF8IpySlFm/86p84tbx0+jMs1niU52VsTscsamGbTzbsxeoHAt1fQUvzYveOGoRezotXblboVB2971r6avMHNtAk0FAdjvh4TjGZJCGTqNHD0mE=</D></RSAKeyValue>";
private static string _publicKey = "<RSAKeyValue><Modulus>poQS/c9tLkgg84xYZpnUBHP6fy24D6XmzhQ8yCOG317hfUNhRt6Z9N4oTn+QcOTh/DAnul4Q901GrHbPrMB8tl1LtbpKbvGftPhyR7OLQVnWC1Oz10t2tHEo7mqyPyAVuYsq8Q1E3YNTh2V6+PRvMiAWGUHGyyG7fKjt/R9W+RE=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>";
private static UnicodeEncoding _encoder = new UnicodeEncoding();
public static string Decrypt(string data)
{
try
{
var rsa = new RSACryptoServiceProvider();
var dataArray = data.Split(new char[] { ',' });
byte[] dataByte = new byte[dataArray.Length];
for (int i = 0; i < dataArray.Length; i++)
{
dataByte[i] = Convert.ToByte(dataArray[i]);
}
rsa.FromXmlString(_privateKey);
var decryptedByte = rsa.Decrypt(dataByte, false);
return _encoder.GetString(decryptedByte);
}
catch (Exception)
{
throw new RSAException();
}
}
public static string Encrypt(string data)
{
try
{
var rsa = new RSACryptoServiceProvider();
rsa.FromXmlString(_publicKey);
var dataToEncrypt = _encoder.GetBytes(data);
var encryptedByteArray = rsa.Encrypt(dataToEncrypt, false).ToArray();
var length = encryptedByteArray.Count();
var item = 0;
var sb = new StringBuilder();
foreach (var x in encryptedByteArray)
{
item++;
sb.Append(x);
if (item < length)
sb.Append(",");
}
return sb.ToString();
}
catch (Exception ex)
{
throw new RSAException();
}
}
public class RSAException : Exception
{
public RSAException() : base("RSA Encryption Error") { }
}
}
定义 TokenValidationHandler
:
public class TokenValidationHandler : DelegatingHandler
{
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
try
{
if (!request.Headers.Contains("Authorization-Token"))
{
return Task<HttpResponseMessage>.Factory.StartNew(() =>
{
return new HttpResponseMessage(HttpStatusCode.BadRequest)
{
Content = new StringContent("You need to include Authorization-Token header in your request")
};
});
}
var 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")
};
});
}
var decryptedToken = RSAClass.Decrypt(token);
// TODO: do your query to find the user
var user = decryptedToken;
var identity = new GenericIdentity(decryptedToken);
string[] roles = new[] { "Users", "Testers" };
var principal = new GenericPrincipal(identity, roles);
Thread.CurrentPrincipal = principal;
}
catch
{
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);
}
}
定义一个测试控制器:
Define a test controller:
public class TestsController : Controller
{
public ActionResult GetProducts()
{
var productsUrl = Url.RouteUrl("DefaultApi", new { httproute = "", controller = "products" }, "http");
using (var client = new HttpClient())
{
var token = RSAClass.Encrypt("john");
client.DefaultRequestHeaders.Add("Authorization-Token", token);
var products = client
.GetAsync(productsUrl)
.Result
.Content
.ReadAsAsync<IEnumerable<Product>>()
.Result;
return Json(products, JsonRequestBehavior.AllowGet);
}
}
public ActionResult PostProduct()
{
var productsUrl = Url.RouteUrl("DefaultApi", new { httproute = "", controller = "products" }, "http");
using (var client = new HttpClient())
{
var token = RSAClass.Encrypt("john");
client.DefaultRequestHeaders.Add("Authorization-Token", token);
var product = new Product
{
Id = 1,
Name = "test product"
};
var result = client
.PostAsync<Product>(productsUrl, product, new JsonMediaTypeFormatter())
.Result;
if (result.StatusCode == HttpStatusCode.Unauthorized)
{
return Content("Sorry you are not authorized to perform this operation");
}
return Json(true, JsonRequestBehavior.AllowGet);
}
}
}
测试:
Test:
* /tests/getproducts => success
* /tests/postproduct => 401
这篇关于如何做asp.net的MVC 4个Web API基于角色的授权的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!