我正在使用angular 1.1.5并且我使用$ resource为一个REST服务创建一个XHR,但似乎$资源没有将标头附加为X-Requested-With作为XMLHttpRequest,是一个正常的行为?我需要手动附加标题吗?
function loginCtrl($scope,$resource) {
$scope.submit = function () {
var resource = $resource('/Api/User/login', {},
{
authenticate: {
method: 'POST',
isArray: false,
headers: {
'__RequestVerificationToken': $scope.loginRequest.Token
}
}
});
resource.authenticate($scope.loginRequest);
};
}
Run Code Online (Sandbox Code Playgroud) 在ASP.NET 5应用程序中,我配置了MVC和Identity框架,如下所示:
app.UseMvc(config=>{
config.MapRoute("Default", "{controller}/{action}/{id?}", new
{
controller = "Home",
action = "Index"
});
});
Run Code Online (Sandbox Code Playgroud)
并添加身份服务:
services.AddAuthentication();
services.AddAuthorization();
services.AddIdentity<CrmUser, CrmUserRole>(config => {
config.User.RequireUniqueEmail = true;
})
.AddUserStore<MongoUserStore>()
.AddRoleStore<MongoUserStore>()
.AddDefaultTokenProviders();
Run Code Online (Sandbox Code Playgroud)
和
app.UseIdentity()
.UseCookieAuthentication(i => { i.LoginPath = "/Account/Login";});
Run Code Online (Sandbox Code Playgroud)
该示例定义如下:
public class MyApiController : Microsoft.AspNet.Mvc.Controller
{
[Authorize]
public async Task<ActionResult> Foo()
{
return Ok();
}
}
Run Code Online (Sandbox Code Playgroud)
这工作正常,但我也有一些控制器,我想以API方式使用.在ASP.NET 5中,它们都具有相同的基类,因此API和视图控制器之间没有区别.
因此,当调用需要授权的未经授权的api时,我会得到一个HTTP 200和登录页面而不是HTTP 401.
在Shawn Wildermuth的博客文章中,我发现了这一点
services.AddCookieAuthentication(config =>
{
config.LoginPath = "/Auth/Login";
config.Events = new CookieAuthenticationEvents()
{ …Run Code Online (Sandbox Code Playgroud) asp.net asp.net-mvc asp.net-identity asp.net-core-mvc asp.net-core
从行动的角度来看,我如何区分ajax请求和常规请求.
puclic class GroupController : Controller
{
public ActionResult AddGroup()
{
if(//regular request...)
return view()
else //an ajax call
return Partial("GroupPartialView)
}
}
Run Code Online (Sandbox Code Playgroud)
我们的想法是,如果将用户添加到不存在的组中,则可以使用对话框添加该组,而无需离开"管理"用户页面.
谢谢你的帮助.