在AAD上注册后自动登录

本文关键字:登录 注册 AAD | 更新日期: 2023-09-27 18:12:04

我想在使用Azure Active Directory成功注册后自动将用户登录到我的应用程序。此时,它将用户重定向到登录页面。

寄存器的代码如下:

    // POST: /Account/Register
    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Register(RegisterViewModel model)
    {
        if (ModelState.IsValid)
        {
            Microsoft.Azure.ActiveDirectory.GraphClient.IUser newUser = new User();
            activeDirectoryClient = AuthenticationHelper.GetActiveDirectoryClientAsApplication();
            newUser.DisplayName = model.Name + " " + model.Surname;
            newUser.UserPrincipalName = model.UserName;
            newUser.AccountEnabled = true;
            newUser.MailNickname = model.Name;                
            newUser.PasswordProfile = new PasswordProfile                
            {
                Password = model.Password,
                ForceChangePasswordNextLogin = false
            };
            newUser.UsageLocation = "US";
            try
            {
                activeDirectoryClient.Users.AddUserAsync(newUser);
            }
            catch (Exception e)
            {
                Console.WriteLine("'nError creating new user {0} {1}", e.Message,
                    e.InnerException != null ? e.InnerException.Message : "");
            }
            return RedirectToAction("Login", "Account");
        }
        // If we got this far, something failed, redisplay form
        return View();
    }

登录代码为:

public void Login()
    {
        // Send an OpenID Connect sign-in request.
        if (!Request.IsAuthenticated)
        {
            HttpContext.GetOwinContext().Authentication.Challenge(new AuthenticationProperties { RedirectUri = "/" }, OpenIdConnectAuthenticationDefaults.AuthenticationType);
        }
    }

任何帮助都将是感激的:)

在AAD上注册后自动登录

根据我的理解,注册和登录是web应用程序的两个不同的概念。预计注册后重定向到登录页面我们不应该混合使用它们。

Azure Active Directory支持使用OpenId connect对依赖方进行身份验证,但不支持未经用户登录的身份验证