I have scaffolded the Identity using the CLI as
dotnet add package Microsoft.VisualStudio.Web.CodeGeneration.Design
dotnet add package Microsoft.EntityFrameworkCore.Design
Then I list all the files
dotnet aspnet-codegenerator identity --listFiles
I used the login dotnet aspnet-codegenerator identity --files="Account.Login"
Now I can see in the Area section the login.cshtml file is generated
Now when I navigate to https://localhost:5001/Identity/Account/Login?ReturnUrl=%2Fconnect%2Fauthorize%2Fcallback%3Fclient_id%3DVotingApplication%26redirect_uri%3Dhttps%253A%252F%252Flocalhost%253A5001%252Fauthentication%252Flogin-callback%26response_type%3Dcode%26scope%3DVotingApplicationAPI%2520openid%2520profile%26state%3Daceb10c45488493b9a675c830327ecbf%26code_challenge%3DnU-0BBK2wjgOSEDdB4Qjy5T8f3qHWTlgu0UmMgVDEAU%26code_challenge_method%3DS256%26response_mode%3Dquery
I get an exception as
InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.Identity.SignInManager`1[Microsoft.AspNetCore.Identity.IdentityUser]' while attempting to activate 'VotingApplication.Areas.Identity.Pages.Account.LoginModel'.
In the startup.cs I have the below configuration
services.AddIdentityServer()
.AddApiAuthorization<ApplicationUser, ApplicationDbContext>()
.AddProfileService<ProfileService>();
Since I am using the ApplicationUser
, I am not able to see the account controller when I do the scaffolder.
public class IdentityHostingStartup : IHostingStartup
{
public void Configure(IWebHostBuilder builder)
{
builder.ConfigureServices((context, services) => {
services.AddDefaultIdentity<ApplicationUser>(options => options.SignIn.RequireConfirmedAccount = true)
.AddEntityFrameworkStores<ApplicationDbContext>();
});
}
}
Since the scaffolder is using IdentityUser
, how can I make this to work with ApplicationUser
_LoginPartial.cshtml
@using Microsoft.AspNetCore.Identity
@using global::Data
@using global::Data.Domain
@inject SignInManager<ApplicationUser> SignInManager
@inject UserManager<ApplicationUser> UserManager
@{
string returnUrl = null;
var query = ViewContext.HttpContext.Request.Query;
if (query.ContainsKey("returnUrl"))
{
returnUrl = query["returnUrl"];
}
}
<ul class="navbar-nav">
@if (SignInManager.IsSignedIn(User))
{
<li class="nav-item">
<a class="nav-link text-dark" asp-area="Identity" asp-page="/Account/Manage/Index" title="Manage">Hello @User.Identity.Name!</a>
</li>
<li class="nav-item">
<form class="form-inline" asp-area="Identity" asp-page="/Account/Logout" asp-route-returnUrl="/">
<button type="submit" class="nav-link btn btn-link text-dark">Logout</button>
</form>
</li>
}
else
{
<li class="nav-item">
<a class="nav-link text-dark" asp-area="Identity" asp-page="/Account/Register" asp-route-returnUrl="@returnUrl">Register</a>
</li>
<li class="nav-item">
<a class="nav-link text-dark" asp-area="Identity" asp-page="/Account/Login" asp-route-returnUrl="@returnUrl">Login</a>
</li>
}
</ul>
InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.Identity.SignInManager`1[Microsoft.AspNetCore.Identity.IdentityUser]' while attempting to activate 'VotingApplication.Areas.Identity.Pages.Account.LoginModel'.
It seems that you update the IdentityUser
derived class with custom properties and name it as ApplicationUser
.
And based on your description and code, we can find that you just replaced IdentityUser
with ApplicationUser
in ConfigureServices
method and _LoginPartial.cshtml
file, you seems not update Login.cshtml.cs
file to replace IdentityUser
with ApplicationUser
, which cause the issue.
Please make sure you inject the instance of UserManager and SignInManager within LoginModel
class using UserManager<ApplicationUser>
and SignInManager<ApplicationUser>
, like below.
[AllowAnonymous]
public class LoginModel : PageModel
{
private readonly UserManager<ApplicationUser> _userManager;
private readonly SignInManager<ApplicationUser> _signInManager;
private readonly ILogger<LoginModel> _logger;
public LoginModel(SignInManager<ApplicationUser> signInManager,
ILogger<LoginModel> logger,
UserManager<ApplicationUser> userManager)
{
_userManager = userManager;
_signInManager = signInManager;
_logger = logger;
}
You have a class that dependents on ApplicationSignInManager. You need to register ApplicationSignInManager
services.AddIdentity<User, Role>(identityOptions =>
{
// identityOptions.SignIn.RequireConfirmedPhoneNumber = true;
// identityOptions.SignIn.RequireConfirmedEmail = true;
})
.AddUserManager<ApplicationUserManager>() // <- use only if use have custom implementation
.AddSignInManager<ApplicationSignInManager>(); // <- use only if use have custom implementation
you can find sample implementation here
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.