简体   繁体   English

使用 UseCookiesAuthentication 来授权 MVC Controller 操作,它在本地工作,当应用程序部署在 azure 中时会出现问题

[英]using UseCookiesAuthentication to authorize the MVC Controller Action, it works locally, gives issue when the application is deployed in azure

I have the MVC application , using the Owin and Asp.Net Identity, and using the useCookieauthentication我有MVC 应用程序,使用 Owin 和 Asp.Net Identity,并使用useCookieauthentication

During login process, I have added the custom claim, and I gets properly sign-in.在登录过程中,我添加了自定义声明,并且可以正确登录。 ``` ```

    [HttpPost]
    public async Task<ActionResult> Login(LoginViewModel loginViewModel)
    {
        var user = UserManager.FindByName(loginViewModel.UserName);
        var signInStatus = UserManager.CheckPassword(user, loginViewModel.Password);

        if (signInStatus)
        {
            user.Claims.Add(new Microsoft.AspNet.Identity.EntityFramework.IdentityUserClaim() { ClaimType = "CustomClaim", ClaimValue = loginViewModel.UserName.Trim() });
            SignInManager.SignIn(user, true, false);
            return Redirect(url);
        }
        else
        {
            ModelState.AddModelError("", "Invalid login attempt.");
            return View(loginViewModel);
        }
    }

When I get the callback to my one of Action method, I try to retrieve Claim Custom Claim that I have stored during login process.当我收到我的 Action 方法之一的回调时,我尝试检索我在登录过程中存储的 Claim Custom Claim。 Locally when I run and debug this code it works correctly as expected.当我在本地运行和调试此代码时,它按预期正常工作。 But when I deploy the application to azure I am unable to get the custom claim value.但是当我将应用程序部署到 azure 时,我无法获得自定义声明值。

       public ActionResult Index()
        {
            var claimsPrincipal = System.Web.HttpContext.Current.User as System.Security.Claims.ClaimsPrincipal;
            var customClaimValue = claimsPrincipal.Identities.First().Claims.First(x => x.Type.Equals("CustomClaim")).Value;
            return View();
        }

Couple of things to try, Let me know if it still doesn't work for you, Posting as per my recent experience which i covered in my other answer.尝试几件事,让我知道它是否仍然不适合您,根据我在其他答案中介绍的最近经验发布。

Also please troubleshoot further to understand more on the inner stack details.还请进一步排除故障以了解有关内部堆栈详细信息的更多信息。

  • As @Joey Cai mentioned in his answer,Change your **Action to take when request is not authenticated in App Service** Authentication/Authorization section in the azure portal from LogIn with Azure Active Directory to **Allow Anonymous requests** .正如@Joey Cai 在他的回答中提到的那样,将您的**Action to take when request is not authenticated in App Service**身份验证/授权部分在 azure 门户中从使用 Azure Active Directory 登录到**Allow Anonymous requests** As shown on the picture below:如下图所示:

在此处输入图像描述

  • If above option doesn't work out try below:如果上述选项不起作用,请尝试以下操作:

    Try changing the application manifest of the application definition on Azure to set the "oauth2AllowIdTokenImplicitFlow" property to true from false.尝试更改 Azure 上应用程序定义的应用程序清单,以将“oauth2AllowIdTokenImplicitFlow”属性从 false 设置为 true。

    • Go to the Azure Portal, Go 到 Azure 门户,
    • Select to Azure Active Directory Select 到 Azure 活动目录
    • Select App Registrations Select 应用注册
    • Select your app. Select 您的应用程序。
    • Click on Manifest点击清单
    • Find the value oauth2AllowIdTokenImplicitFlow and change it's value to true找到值 oauth2AllowIdTokenImplicitFlow 并将其值更改为 true
    • Click Save点击保存

Asp.net UseOpenIdConnectAuthentication not working in Azure Asp.net UseOpenIdConnectAuthentication 在 Azure 中不起作用

Hope it helps.希望能帮助到你。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 简单的WebClient在本地工作,但在部署到Azure时无法工作 - Simple WebClient works locally, but not when deployed to Azure 将映像上传到Azure Blob存储-本地工作,部署后失败 - Uplod images to azure blob storage - Works locally, fails when deployed C# Azure ServiceBus / Azure SQL - 在本地工作,部署到 Azure 时似乎不起作用 - C# Azure ServiceBus / Azure SQL - works locally, does not seem to work when deployed to Azure 自定义授权在操作级别上起作用,但不在控制器级别上起作用 - Custom Authorize works on action level but not on controller level 在Azure中部署MVC应用程序RoleManager后出现问题 - MVC Application RoleManager issue after being deployed in Azure JavaScript在本地运行,但在部署到Web服务器时无法运行 - JavaScript works locally but not when deployed to webserver Database First DBContext在本地工作,但在部署时无法工作 - Database First DBContext works locally but not when deployed 当应用程序部署为 Azure Web 应用程序时,OnTokenValidated 事件未触发,但在本地运行时触发 - OnTokenValidated event not firing when application is deployed as Azure Web App, but fires when running locally Web api在本地工作,但在部署到Azure时则不能 - Web api working locally but not when deployed to Azure 部署后,Azure 函数中的依赖注入失败但在本地工作 - Dependency Injection failing in Azure function once deployed but works locally
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM