为什么是AspNet.Identity 3.0登录后User.Identity空 [英] Why is User.Identity null after login with AspNet.Identity 3.0
问题描述
我使用DNX RC1内微软AspNet.Identity 3.0框架。随着一些教程的帮助我建立了一个自定义的验证系统。一个成功的密码后查了一些索赔为用户创建和认证将被设置:
VAR claimsPrincipal =等待SignInManager.CreateUserPrincipalAsync(用户);
如果(claimsPrincipal = NULL&放大器;!&安培;!claimsPrincipal.Identity = NULL)
{
//设置权利要求向用户
等待HttpContext.Authentication.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme,claimsPrincipal);
返回RedirectToAction(指数,应用程序);
}
在此登录操作我的浏览器有两个cookie: .AspNet.Cookies 的和的 .AspNet.Microsoft.AspNet.Identity.Application 的
不过,我现在有我的身份有问题。与[授权]注解控制器没有执行。并通过[使用AllowAnonymous]控制器给我一个NullReferenceException因为User.Identity为空:
[使用AllowAnonymous]
[路线(API /车次)]
公共类TripController:控制器
{[HTTPGET()]
公共JsonResult获得()
{
VAR车次= _repository.GetUserTripsWithStops(User.Identity.Name);
... 返回JSON(结果);
}
有人可以告诉我有什么错我的认证?
当我想,我的错误是某处Startup.cs文件 - 这里是配置方法:
公共无效配置(IApplicationBuilder应用程序)
{
app.UseStaticFiles(); app.UseIdentity();
app.UseCookieAuthentication(选项=>
{
options.LoginPath =新PathString(/应用程序/登录);
}); app.UseMvc(路线=>
{
routes.MapRoute(
名称:默认,
模板:{控制器} / {行动} / {?ID}
默认:新{控制器=应用程序,行动=索引});
});
}
感谢上帝,我发现经过一天多的试验和错误的解决方案。最后,我刚刚添加的AutomaticAuthenticate线在Startup.cs文件:
app.UseCookieAuthentication(选项=>
{
options.AutomaticAuthenticate = TRUE;
options.LoginPath =新PathString(/应用程序/登录);
});
I am using Microsofts AspNet.Identity 3.0 framework within the DNX RC1. With the help of some tutorials I have built a custom authentication system. After a successful password check some claims are created for the user and the Authentication will be set:
var claimsPrincipal = await SignInManager.CreateUserPrincipalAsync(user);
if (claimsPrincipal != null && claimsPrincipal.Identity != null)
{
// Set the claims to the user
await HttpContext.Authentication.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, claimsPrincipal);
return RedirectToAction("Index", "App");
}
After this login action my browser has two cookies: .AspNet.Cookies and .AspNet.Microsoft.AspNet.Identity.Application
However I do have now a problem with my identity. Controllers annotated with [Authorize] are not executed at all. And controllers with [AllowAnonymous] give me a NullReferenceException because User.Identity is null:
[AllowAnonymous]
[Route("api/trips")]
public class TripController : Controller
{
[HttpGet("")]
public JsonResult Get()
{
var trips = _repository.GetUserTripsWithStops(User.Identity.Name);
...
return Json(results);
}
Can someone please tell me what's wrong with my authentication?
As I guess that my mistake is somewhere in the Startup.cs file - here is the configure method:
public void Configure(IApplicationBuilder app)
{
app.UseStaticFiles();
app.UseIdentity();
app.UseCookieAuthentication(options =>
{
options.LoginPath = new PathString("/App/Login");
});
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller}/{action}/{id?}",
defaults: new { controller = "App", action = "Index" });
});
}
Thanks god I have found the solution after more than one day trial and error. Finally I just added the AutomaticAuthenticate-line in the Startup.cs file:
app.UseCookieAuthentication(options =>
{
options.AutomaticAuthenticate = true;
options.LoginPath = new PathString("/App/Login");
});
这篇关于为什么是AspNet.Identity 3.0登录后User.Identity空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!