以下代码在我的本地计算机上运行正常,但是一旦我部署它DependencyResolver.Current.GetService<ApplicationUserManager>()
返回null,导致这个:
[NullReferenceException: Object reference not set to an instance of an object.]
xxxx.API.Providers.<GrantResourceOwnerCredentials>d__2.MoveNext() in
D:\xxxx\xxxx.API\Providers\ApplicationOAuthProvider.cs:36
调用userManager.FindAsync(context.UserName, context.Password)
时。请告知我的下一步应该是什么?我甚至不确定从哪里开始查看服务器日志。
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
var userManager = DependencyResolver.Current.GetService<ApplicationUserManager>();
var roleManager = DependencyResolver.Current.GetService<ApplicationRoleManager>();
ApplicationUser user = await userManager.FindAsync(context.UserName, context.Password);
if (user == null)
{
context.SetError("invalid_grant", "The user name or password is incorrect.");
return;
}
var userRoles = user.Roles.Select(r => r.RoleId);
ClaimsIdentity oAuthIdentity = await user.GenerateUserIdentityAsync(userManager,
OAuthDefaults.AuthenticationType);
ClaimsIdentity cookiesIdentity = await user.GenerateUserIdentityAsync(userManager,
CookieAuthenticationDefaults.AuthenticationType);
oAuthIdentity.AddClaims(roleManager.Roles.Where(r => userRoles.Any(ur => ur == r.Id))
.Select(r => r.Claims).ToList()
.Select(c => new Claim("RoleClaims", c.ToString())));
AuthenticationProperties properties = CreateProperties(user.UserName);
AuthenticationTicket ticket = new AuthenticationTicket(oAuthIdentity, properties);
context.Validated(ticket);
context.Request.Context.Authentication.SignIn(cookiesIdentity);
}
玩我的部署并发现当我将API部署到我正在托管的匹配网站的单独文件夹时,不会出现问题(我的API和网站是两个独立的项目)。
因此,当我将网站部署到xxxx和API到xxxx / API时,会出现上述问题。
当我将网站部署到xxxx和API到xxxxAPI时,不会发生上述问题。
我本地计算机上的托管路径镜像远程的托管路径,所以我认为这是一个配置问题。这是否进一步向任何人表明问题可能是什么?