I am developing a custom login page for Dynamics CRM 365 On-Premise using Blazor. The login page works correctly for authenticating users, but after authentication, when the user is redirected to the main CRM page, they are prompted to log in again.
Here’s the relevant code for my custom login page:
using xxx.Models.Pages;using Blazored.Modal;using Blazored.Modal.Services;using Microsoft.AspNetCore.Components;using System.Net;namespace xxx.Components.Pages{ public partial class Login : ComponentBase { private bool Isbusy; [CascadingParameter] public IModalService Modal { get; set; } = default!; [SupplyParameterFromForm] public LoginRequest loginModel { get; set; } protected override void OnInitialized() => loginModel ??= new LoginRequest(); private async Task HandleValidSubmit() { if (Isbusy) return; Isbusy = true; if (!AuthService.ValidateCredentials(loginModel.UserName, loginModel.Password)) { ShowModal("نامکاربرییاکلمهعبورصحیحنمیباشد"); Isbusy = false; return; } if (!loginModel.UserName.Contains(loginModel.Password) && loginModel.Password.Length >= 8) { await AuthenticateAndRedirectAsync(); Isbusy = false; return; } IModalReference modal = ShowModal("ابتدااقدامبهتغییررمزخودکنید"); var result = await modal.Result; if (!result.Cancelled) navigationManager.NavigateTo("changepassword"); Isbusy = false; } public async Task AuthenticateAndRedirectAsync() { var handler = new HttpClientHandler { Credentials = new NetworkCredential(loginModel.UserName, loginModel.Password, "xxxxx.local"), PreAuthenticate = true, UseDefaultCredentials = false }; using (var client = new HttpClient(handler)) { var request = new HttpRequestMessage(HttpMethod.Get, "http://xxx.xxx.xxx.xx:82/CRMxxxxx/api/data/v9.1/accounts"); var response = await client.SendAsync(request); if (!response.IsSuccessStatusCode) { var error = await response.Content.ReadAsStringAsync(); ShowModal($"Authentication failed: {response.StatusCode} - {error}"); return; } navigationManager.NavigateTo("http://xxx.xxx.xxx.xx:82/CRMxxxxx/main.aspx", true); } } private IModalReference ShowModal(string message) { var options = new ModalOptions { AnimationType = ModalAnimationType.FadeInOut, Position = ModalPosition.Middle, DisableBackgroundCancel = false, HideHeader = true }; var parameters = new ModalParameters { { nameof(ModalComponent.Message), message }, { nameof(ModalComponent.ShowButton), true } }; var modal = Modal.Show<ModalComponent>(parameters, options); return modal; } }}Issue:
- Users are authenticated correctly through the custom login page.
- However, upon being redirected to the CRM main page, they are repeatedly prompted to enter their username and password again.
What I Tried:
Custom Login Implementation: I implemented a custom login page that authenticates users using
HttpClientwithNetworkCredential.Redirection After Authentication: After a successful login, I redirect the user to the CRM main page.
Error Handling: Added error handling to display appropriate messages if authentication fails.
What I Expected:
I expected that after a successful login, the user would be redirected to the CRM main page without being asked to log in again.
What Actually Resulted:
Despite successful authentication, users are prompted to log in again after being redirected to the CRM main page. It seems that the CRM system does not recognize the authenticated state from the custom login page.
Questions:
- Why does the CRM page prompt for credentials again after a successful login?
- How can I ensure that the CRM session is properly maintained after redirection?
- Are there any additional steps or configurations needed to manage sessions or cookies with Dynamics CRM On-Premise?
Any assistance or insights would be greatly appreciated. Thank you!
What I Tried:
Implemented Custom Login Page: I created a custom login page using Blazor where users enter their credentials. The authentication is performed via an
HttpClientwithNetworkCredential, and a successful login is confirmed by accessing a CRM API endpoint.Redirection After Authentication: After successful authentication, I redirect users to the CRM main page using
NavigationManager.NavigateTo.Error Handling: Incorporated error handling to show modal messages in case of authentication failures.
What I Expected:
I expected that after a user successfully logs in through the custom login page, they would be redirected to the CRM main page without having to log in again. The CRM system should recognize the authenticated session and allow access without further credential prompts.
What Actually Resulted:
Even though the user is authenticated successfully and redirected to the CRM main page, the CRM application prompts for username and password again. It appears that the CRM does not recognize the authentication state established by the custom login page, resulting in repeated login prompts.