Reg*_*Rom 1 c# azure azure-active-directory azure-web-app-service asp.net-core
所以我正在开发一个 ASP.NET Core 应用程序 (.NET Core 2.0),在 Azure 上作为应用服务托管。我想使用单个租户(因此只有我们公司的帐户)通过 Azure AD 实现身份验证。我实际上添加了所有必要的代码,注册了应用程序并在应用服务中配置了所有内容(至少我是这么认为的),当我在本地运行它时,它甚至没有任何问题。当我将应用程序发布到 Azure 并尝试在那里登录时会出现问题。我没有被重定向到我选择的视图,而是被重定向到“~/.auth/login/done”,我看到了这个:https ://imgur.com/a/6OeKUNy
正如我所说,我注册了应用程序并添加了应用程序网址和回复网址,它们看起来像这样:
我在身份验证/授权部分配置了应用服务本身,并使用高级设置。当前,客户端密钥和允许的令牌受众字段为空。我不希望用户在进入网站后立即进行身份验证,但是当他单击某个按钮时,所以我设置了“允许匿名请求(无操作)”。我添加了必要的代码使其工作:
帐户控制器:
[Authorize]
[Route("[controller]/[action]")]
public class AccountController : Controller
{
private readonly OmsIntegrationContext _context;
private readonly IUserService _userService;
public AccountController(OmsIntegrationContext context, IUserService userservice)
{
_context = context;
_userService = userservice;
}
[HttpGet]
[AllowAnonymous]
public IActionResult LoginAzureAd(string returnUrl)
{
var redirectUrl = Url.Action(nameof(ChangeRequestController.Index), "ChangeRequest");
return Challenge(new AuthenticationProperties { RedirectUri = redirectUrl },
OpenIdConnectDefaults.AuthenticationScheme);
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task LogoutAzureAd()
{
if (User.Identity.IsAuthenticated)
{
await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
await HttpContext.SignOutAsync(OpenIdConnectDefaults.AuthenticationScheme);
}
}
Run Code Online (Sandbox Code Playgroud)
启动.cs:
services.AddAuthentication(x =>
{
x.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
x.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
x.DefaultSignOutScheme = CookieAuthenticationDefaults.AuthenticationScheme;
x.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
})
.AddOpenIdConnect(options =>
{
var azureAdConfig = Configuration.GetSection("AzureAd");
options.Authority = azureAdConfig.GetValue<string>("Instance") +
azureAdConfig.GetValue<string>("Domain");
options.ClientId = azureAdConfig.GetValue<string>("ClientId");
options.ResponseType = OpenIdConnectResponseType.IdToken;
options.CallbackPath = azureAdConfig.GetValue<string>("Callback");
options.SignedOutRedirectUri = "https://appname.azurewebsites.net/";
options.TokenValidationParameters.NameClaimType = "name";
options.UseTokenLifetime = true;
})
.AddCookie()
.AddSalesforceAuthentications(AuthConfigs);
services.ConfigureApplicationCookie(x =>
{
x.Cookie.Name = ".SalesForce.Cookie"; //This isn't my code. Could this cause problems?
});
Run Code Online (Sandbox Code Playgroud)
注意:本项目一般使用salesforce auth,Azure AD应该只对某个模块起作用,适用于没有SF账号的用户。这不是我的主意,但我必须这样做
appsettings.json:
"AzureAd": {
"ClientId": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
"TenantId": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
"Instance": "https://login.microsoftonline.com/",
"Domain": "XXXXX.onmicrosoft.com",
"Callback": "/.auth/login/aad/callback"
}
Run Code Online (Sandbox Code Playgroud)
我想要实现的以及在本地主机上运行时实现的是,当用户进入 Web 应用程序时,他单击一个按钮,使用公司帐户通过 Azure AD 进行身份验证,然后重定向到 ~/ChangeRequest/Index。但我无法在部署后使其工作。我在这里发现了类似的问题:
但这个家伙解决它的方式对我来说不是一个选择。有任何想法吗?
我设法解决了这个问题。显然,不能同时在应用服务和代码中配置此身份验证。我所要做的就是在应用服务的“身份验证/授权”部分关闭 Azure 门户上的身份验证。这是我关于 stackoverflow 的第一个问题,我真的不知道是否应该删除这个问题。如果是这样,请告诉我。
| 归档时间: |
|
| 查看次数: |
1259 次 |
| 最近记录: |