private async Task <ActionResult> ExternalSignInAsync(ExternalLoginInfo loginInfo, Func <ActionResult> response) { if (loginInfo == null) { throw new ArgumentNullException("loginInfo"); } if (response == null) { throw new ArgumentNullException("response"); } ExternalSignInAutoLinkOptions autoLinkOptions = null; // Here we can check if the provider associated with the request has been configured to allow // new users (auto-linked external accounts). This would never be used with public providers such as // Google, unless you for some reason wanted anybody to be able to access the backend if they have a Google account // .... not likely! var authType = OwinContext.Authentication.GetExternalAuthenticationTypes().FirstOrDefault(x => x.AuthenticationType == loginInfo.Login.LoginProvider); if (authType == null) { Logger.Warn <BackOfficeController, string>("Could not find external authentication provider registered: {LoginProvider}", loginInfo.Login.LoginProvider); } else { autoLinkOptions = authType.GetExternalSignInAutoLinkOptions(); } // Sign in the user with this external login provider if the user already has a login var user = await UserManager.FindAsync(loginInfo.Login); if (user != null) { var shouldSignIn = true; if (autoLinkOptions != null && autoLinkOptions.OnExternalLogin != null) { shouldSignIn = autoLinkOptions.OnExternalLogin(user, loginInfo); if (shouldSignIn == false) { Logger.Warn <BackOfficeController, string, int>("The AutoLinkOptions of the external authentication provider '{LoginProvider}' have refused the login based on the OnExternalLogin method. Affected user id: '{UserId}'", loginInfo.Login.LoginProvider, user.Id); } } if (shouldSignIn) { //sign in await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false); } } else { if (await AutoLinkAndSignInExternalAccount(loginInfo, autoLinkOptions) == false) { ViewData.SetExternalSignInProviderErrors( new BackOfficeExternalLoginProviderErrors( loginInfo.Login.LoginProvider, new[] { "The requested provider (" + loginInfo.Login.LoginProvider + ") has not been linked to an account" })); } //Remove the cookie otherwise this message will keep appearing if (Response.Cookies[Constants.Security.BackOfficeExternalCookieName] != null) { Response.Cookies[Constants.Security.BackOfficeExternalCookieName].Expires = DateTime.MinValue; } } return(response()); }
private async Task<ActionResult> ExternalSignInAsync(ExternalLoginInfo loginInfo, Func<ActionResult> response) { if (loginInfo == null) throw new ArgumentNullException("loginInfo"); if (response == null) throw new ArgumentNullException("response"); ExternalSignInAutoLinkOptions autoLinkOptions = null; //Here we can check if the provider associated with the request has been configured to allow // new users (auto-linked external accounts). This would never be used with public providers such as // Google, unless you for some reason wanted anybody to be able to access the backend if they have a Google account // .... not likely! var authType = OwinContext.Authentication.GetExternalAuthenticationTypes().FirstOrDefault(x => x.AuthenticationType == loginInfo.Login.LoginProvider); if (authType == null) { Logger.Warn<BackOfficeController>("Could not find external authentication provider registered: {LoginProvider}", loginInfo.Login.LoginProvider); } else { autoLinkOptions = authType.GetExternalAuthenticationOptions(); } // Sign in the user with this external login provider if the user already has a login var user = await UserManager.FindAsync(loginInfo.Login); if (user != null) { // TODO: It might be worth keeping some of the claims associated with the ExternalLoginInfo, in which case we // wouldn't necessarily sign the user in here with the standard login, instead we'd update the // UseUmbracoBackOfficeExternalCookieAuthentication extension method to have the correct provider and claims factory, // ticket format, etc.. to create our back office user including the claims assigned and in this method we'd just ensure // that the ticket is created and stored and that the user is logged in. var shouldSignIn = true; if (autoLinkOptions != null && autoLinkOptions.OnExternalLogin != null) { shouldSignIn = autoLinkOptions.OnExternalLogin(user, loginInfo); if (shouldSignIn == false) { Logger.Warn<BackOfficeController>("The AutoLinkOptions of the external authentication provider '{LoginProvider}' have refused the login based on the OnExternalLogin method. Affected user id: '{UserId}'", loginInfo.Login.LoginProvider, user.Id); } } if (shouldSignIn) { //sign in await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false); } } else { if (await AutoLinkAndSignInExternalAccount(loginInfo, autoLinkOptions) == false) { ViewData.SetExternalSignInError(new[] { "The requested provider (" + loginInfo.Login.LoginProvider + ") has not been linked to an account" }); } //Remove the cookie otherwise this message will keep appearing if (Response.Cookies[Constants.Security.BackOfficeExternalCookieName] != null) { Response.Cookies[Constants.Security.BackOfficeExternalCookieName].Expires = DateTime.MinValue; } } return response(); }