Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Azure上asp.net mvc OAuth外部登录失败(本地主机正常)_C#_Asp.net_Asp.net Mvc_Azure_Oauth 2.0 - Fatal编程技术网

C# Azure上asp.net mvc OAuth外部登录失败(本地主机正常)

C# Azure上asp.net mvc OAuth外部登录失败(本地主机正常),c#,asp.net,asp.net-mvc,azure,oauth-2.0,C#,Asp.net,Asp.net Mvc,Azure,Oauth 2.0,在部署到Azure之后,我遇到了外部登录提供商的问题,我已经能够通过外部提供商(google和facebook)进行身份验证,但当用户单击注册时收到错误 我遵循本教程使用“成员API”并添加“canEdit”角色 使用成员API 它在localhost上运行良好,但当我部署到Azure时,它成功地向Google或FB进行身份验证,并访问externalloginconconfirmation.cshtml并允许用户按register,然后显示一个错误声明 “处理您的请求时出错。” 我可以连接到

在部署到Azure之后,我遇到了外部登录提供商的问题,我已经能够通过外部提供商(google和facebook)进行身份验证,但当用户单击注册时收到错误

我遵循本教程使用“成员API”并添加“canEdit”角色

使用成员API

它在localhost上运行良好,但当我部署到Azure时,它成功地向Google或FB进行身份验证,并访问externalloginconconfirmation.cshtml并允许用户按register,然后显示一个错误声明

“处理您的请求时出错。”

我可以连接到数据库上的Azure ASPNetUsers表,我可以确认用户已经创建,用户可以实际单击登录并进入站点。但是,用户尚未添加到“canEdit”角色,因此在确认过程中,该角色似乎部分完成或失败。我不明白为什么这在本地主机上有效,但在Azure上不起作用,我也为Azure构建了一个新的部署。错误是否可能在用于外部登录确认的Accountcontroller方法中?将用户添加到“canEdit”角色时,而不是在本地主机上,仅在Azure上,似乎会发生此错误

// POST: /Account/ExternalLoginConfirmation
    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl)
    {
        if (User.Identity.IsAuthenticated)
        {
            return RedirectToAction("Index", "Manage");
        }

        if (ModelState.IsValid)
        {
            // Get the information about the user from the external login provider
            var info = await AuthenticationManager.GetExternalLoginInfoAsync();
            if (info == null)
            {
                return View("ExternalLoginFailure");
            }
            var user = new ApplicationUser {
                UserName = model.DisplayName,
                Email = model.Email,
                OAuthName = model.OAuthName,
                DisplayName = model.DisplayName,
                ForceID = model.ForceID,
                NeighID = model.NeighID,
                MyLatLng = model.MyLatLng
            };
            var result = await UserManager.CreateAsync(user);
            if (result.Succeeded)
            {
                result = await UserManager.AddLoginAsync(user.Id, info.Login);
                if (result.Succeeded)
                {
                    await UserManager.AddToRoleAsync(user.Id, "canEdit");
                    await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);
                    return RedirectToLocal(returnUrl);
                }
            }
            AddErrors(result);
        }

        ViewBag.ReturnUrl = returnUrl;
        return View(model);
    }
//POST:/Account/外部登录确认
[HttpPost]
[异名]
[ValidateAntiForgeryToken]
公共异步任务ExternalLoginConfirmation(ExternalLoginConfirmationViewModel模型,字符串返回URL)
{
if(User.Identity.IsAuthenticated)
{
返回重定向操作(“索引”、“管理”);
}
if(ModelState.IsValid)
{
//从外部登录提供程序获取有关用户的信息
var info=await AuthenticationManager.GetExternalLoginInfoAsync();
if(info==null)
{
返回视图(“外部登录失败”);
}
var user=新应用程序用户{
用户名=model.DisplayName,
Email=model.Email,
OAuthName=model.OAuthName,
DisplayName=model.DisplayName,
ForceID=model.ForceID,
NeighID=model.NeighID,
MyLatLng=model.MyLatLng
};
var result=await UserManager.CreateAsync(用户);
if(result.successed)
{
结果=wait UserManager.AddLoginAsync(user.Id,info.Login);
if(result.successed)
{
等待UserManager.AddToRoleAsync(user.Id,“canEdit”);
等待SignInManager.SignInAsync(用户,isPersistent:false,rememberBrowser:false);
返回重定向到本地(returnUrl);
}
}
加法器(结果);
}
ViewBag.ReturnUrl=返回URL;
返回视图(模型);
}

有人能推荐基本的调试步骤吗?有没有办法查看错误日志?(抱歉,因为我是新手-感谢指导)。

检查Azure数据库后,我发现新角色不在“AspNetRoles”表中,因此它正在将用户添加到“AspNetUsers”表中,但在尝试分配角色时给出了错误

对于任何可能有类似问题的人,需要检查的3个表是:

AspNetRoles - check all 'roles' are present 
AspNetUsers - check all 'users' are present
AspNetUserRoles - check user and associated roles

我的错误在于发布应用程序的方式,在“发布Web向导”的“设置”选项卡上,我没有选择“执行代码优先迁移”

我不明白。你的问题是只使用Azure吗?然后你说“…(谷歌和facebook)但是当用户点击注册时收到一个错误。”。我建议您首先确定您的问题。嗨,Ozgur,是的,问题只出现在Azure上。在本地主机上,一切正常,用户通过OAuth提供程序的身份验证,并成功地输入到Aspnetusers表中,用户还分配了新的“CanEdit”角色。我在Azure上遇到了这个问题,用户通过FB和Google的身份验证,进入ExternalLoginConfirmation页面,按register时出现错误。用户可以单击login并登录到应用程序中,在检查Azure DB时,用户位于Aspnetuser表中,但不具有新的“canedit”角色。我已经在Azure上部署了两次,包括一个新项目。我是Google&Facebook,作为OAuth2.0提供商,为用户提供身份验证。