Boa*_*ler 6 asp.net asp.net-mvc asp.net-identity asp.net-core-mvc asp.net-core
在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()
{
OnRedirect = ctx =>
{
if (ctx.Request.Path.StartsWithSegments("/api") &&
ctx.Response.StatusCode == 200)
{
ctx.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
return Task.FromResult<object>(null);
}
else
{
ctx.Response.Redirect(ctx.RedirectUri);
return Task.FromResult<object>(null);
}
}
};
});
Run Code Online (Sandbox Code Playgroud)
但这应该是预期的方式吗?对我来说这闻起来有点香.
此问题已在 中修复RC1。
在这里查看 GitHub 评论:问题
要升级到RC1,请访问http://get.asp.net。
更确定的是,您还可以%userprofile%\.dnx\在获取最新版本之前清除文件夹。
| 归档时间: |
|
| 查看次数: |
1166 次 |
| 最近记录: |