【发布时间】:2016-03-21 09:37:07
【问题描述】:
我们正在尝试创建以下场景:
Html 页面向 WebApi 发送 ajax 请求,询问用户是否具有特定的用户角色。 WebApi 从 ADFS 检查用户是否登录(如果没有,WebApi 对用户进行身份验证)。 WebApi 然后从 ADFS 读取用户角色并将 true/false 返回到 html 页面。
我们目前所拥有的:
Ajax 向 WebApi 发送 Get-request。在 WebApi 中,我们有 Authorize-tag,它可以正确地将用户发送到 ADFS-authentication。但是,在身份验证之后,ADFS 将包含 saml 信息的 html 页面返回给客户端而不是 WebApi。所以现在我们创建另一个 ajax 请求(这次是发布),它接收了 html-page 作为数据。 WebApi 然后对其进行解析并根据 SAML 响应中的用户角色返回真/假。
问题:
当前使用的机制似乎很笨重。这种机制是正确的还是有更好的方法来做到这一点?
如果我们使用上述方法,是否有可能用户编辑收到的 html 页面并赋予自己他实际上没有的角色?
- 即使上述机制是正确的,当 WebApi 重定向到身份验证时,我们仍然会遇到 cors-error。 Cors 在 WebApi 的 Startup.cs 中启用。我们如何摆脱这种情况?
代码:
阿贾克斯:
var uri = 'api/user';
$(document).ready(function () {
$.ajax({
url: uri,
type: "Get",
success: function (data) {
$.ajax({
url: uri,
type: "POST",
data: data,
success: function (value) {
if (value == true) {
$('#userData').text('You have correct role.');
}
else {
$('#userData').text('You don't have correct role.');
}
},
error: function (jqXHR, textStatus, err) {
window.location.href = "NotLoggedIn.html";
}
})
},
error: function (jqXHR, textStatus, err) {
window.location.href = "NotLoggedIn.html";
}
});
});
Startup.Auth.cs:
public void ConfigureAuth(IAppBuilder app)
{
app.SetDefaultSignInAsAuthenticationType(WsFederationAuthenticationDefaults.AuthenticationType);
app.UseWsFederationAuthentication(new WsFederationAuthenticationOptions
{
MetadataAddress = ConfigurationManager.AppSettings["ida:AdfsMetadataEndpoint"],
Wtrealm = ConfigurationManager.AppSettings["ida:Audience"],
});
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = WsFederationAuthenticationDefaults.AuthenticationType
});
}
用户控制器:
namespace SSOWebApiSample.Controllers{
[RoutePrefix("api/user")]
public class UserController : ApiController
{
[HttpGet]
[Route("")]
[Authorize]
public IHttpActionResult GetUser()
{
return Ok();
}
[HttpPost]
[Route("")]
public async Task<IHttpActionResult> PostUser()
{
bool isAdditionalInfoAllowedUser = false;
string result = await Request.Content.ReadAsStringAsync();
//Parse result here
return Ok(isAdditionalInfoAllowedUser);
}
}
}
【问题讨论】:
标签: c# asp.net ajax asp.net-web-api adfs