I'm trying to override the default asp.net identity with my own ( a DB already exist or authentication N authorization)
said that, i've created my own userstore and rolestore. the startup.cs file is:
``` C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.Routing;
using CustomAuth.Models;
using Microsoft.AspNetCore.Http;
namespace CustomAuth
{
public class Startup
{
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);
if (env.IsEnvironment("Development"))
{
// This will push telemetry data through Application Insights pipeline faster, allowing you to view results immediately.
builder.AddApplicationInsightsSettings(developerMode: true);
}
builder.AddEnvironmentVariables();
Configuration = builder.Build();
}
public IConfigurationRoot Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddApplicationInsightsTelemetry(Configuration);
services.AddIdentity<AMEUser, AMEUserRole>()
.AddUserStore<AMEUserStore>()
.AddUserManager<AMEUserManager>()
.AddDefaultTokenProviders();
services.AddScoped<Microsoft.AspNetCore.Identity.SignInManager<AMEUser>, ChiaouSingnInManager>();
services.AddMvc();
//services.Configure<CookieAuthenticationOptions>(options =>
//{
// options.LoginPath = new PathString("/Account/Login");
//});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
//app.UseCookieAuthentication();
app.UseApplicationInsightsRequestTelemetry();
app.UseApplicationInsightsExceptionTelemetry();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Account}/{action=Login}/{id?}");
});
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
LoginPath = new PathString("/Account/Login")
});
}
}
}
and project.json file is
``` js
{
"dependencies": {
"EntityFramework": "6.1.3",
"Microsoft.ApplicationInsights.AspNetCore": "1.0.0",
"Microsoft.AspNet.Identity.Core": "2.2.1",
"Microsoft.AspNetCore.Identity": "1.0.0",
"Microsoft.AspNetCore.Mvc": "1.0.1",
"Microsoft.AspNetCore.Routing": "1.0.1",
"Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
"Microsoft.AspNetCore.Server.Kestrel": "1.0.1",
"Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0",
"Microsoft.Extensions.Configuration.FileExtensions": "1.0.0",
"Microsoft.Extensions.Configuration.Json": "1.0.0",
"Microsoft.Extensions.Logging": "1.0.0",
"Microsoft.Extensions.Logging.Console": "1.0.0",
"Microsoft.Extensions.Logging.Debug": "1.0.0",
"Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0"
},
"tools": {
"Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final"
},
"frameworks": {
"net452": { }
},
"buildOptions": {
"emitEntryPoint": true,
"preserveCompilationContext": true
},
"publishOptions": {
"include": [
"wwwroot",
"**/*.cshtml",
"appsettings.json",
"web.config"
]
},
"scripts": {
"postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
}
}
and my user and role store 👍
``` C#
public class AMEUserStore : Microsoft.AspNetCore.Identity.IUserStore
{
private AMEDbContext database;
public AMEUserStore()
{
database = new AMEDbContext();
}
public void Dispose()
{
database.Dispose();
}
#region userstore
public Task CreateAsync(AMEUser user)
{
throw new NotImplementedException();
}
public Task<Microsoft.AspNetCore.Identity.IdentityResult> CreateAsync(AMEUser user, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
public Task DeleteAsync(AMEUser user)
{
throw new NotImplementedException();
}
public Task<Microsoft.AspNetCore.Identity.IdentityResult> DeleteAsync(AMEUser user, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
public async Task<AMEUser> FindByIdAsync(string userId)
{
var user = await database.Users.Where(c => c.Id == userId).FirstOrDefaultAsync();
return user;
}
Task<AMEUser> Microsoft.AspNetCore.Identity.IUserStore<AMEUser>.FindByIdAsync(string userId, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
public async Task<AMEUser> FindByNameAsync(string userName)
{
var user = await database.Users.Where(c => c.UserName == userName).FirstOrDefaultAsync();
return user;
}
async Task<AMEUser> Microsoft.AspNetCore.Identity.IUserStore<AMEUser>.FindByNameAsync(string userName, CancellationToken cancellationToken)
{
var user = await database.Users.Where(c => c.UserName == userName).FirstOrDefaultAsync();
return user;
}
public async Task<string> GetNormalizedUserNameAsync(AMEUser user, CancellationToken cancellationToken)
{
var user1 = await database.Users.Where(c => c.UserName == user.UserName).FirstOrDefaultAsync();
return user1.UserName;
}
public async Task<string> GetUserIdAsync(AMEUser user, CancellationToken cancellationToken)
{
var user1 = await database.Users.Where(c => c.Id == user.Id).FirstOrDefaultAsync();
return user1.Id;
}
public async Task<string> GetUserNameAsync(AMEUser user, CancellationToken cancellationToken)
{
var user1 = await database.Users.Where(c => c.UserName == user.UserName).FirstOrDefaultAsync();
return user1.UserName;
}
public Task SetNormalizedUserNameAsync(AMEUser user, string normalizedName, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
public Task SetUserNameAsync(AMEUser user, string userName, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
public Task UpdateAsync(AMEUser user)
{
throw new NotImplementedException();
}
public Task<Microsoft.AspNetCore.Identity.IdentityResult> UpdateAsync(AMEUser user, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
#endregion
#region RoleStore
public Task<Microsoft.AspNetCore.Identity.IdentityResult> CreateAsync(AMEUserRole role, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
public Task<Microsoft.AspNetCore.Identity.IdentityResult> DeleteAsync(AMEUserRole role, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
public async Task<AMEUserRole> FindByIdAsync(string roleId, CancellationToken cancellationToken)
{
var user = await database.Roles.Where(c => c.Id == roleId).FirstOrDefaultAsync();
return await Task.FromResult(user);
}
public Task<AMEUserRole> FindByNameAsync(string normalizedRoleName, CancellationToken cancellationToken) {
throw new NotImplementedException();
}
public Task<string> GetNormalizedRoleNameAsync(AMEUserRole role, CancellationToken cancellationToken) {
throw new NotImplementedException();
}
public async Task<string> GetRoleIdAsync(AMEUserRole role, CancellationToken cancellationToken) {
var user1 = await database.Roles.Where(c => c.Id == role.Id).FirstOrDefaultAsync();
return user1.Id;
}
public async Task<string> GetRoleNameAsync(AMEUserRole role, CancellationToken cancellationToken) {
var user1 = await database.Roles.Where(c => c.Name == role.Name).FirstOrDefaultAsync();
return user1.Name;
}
public Task SetNormalizedRoleNameAsync(AMEUserRole role, string normalizedName, CancellationToken cancellationToken) {
throw new NotImplementedException();
}
public Task SetRoleNameAsync(AMEUserRole role, string roleName, CancellationToken cancellationToken) {
throw new NotImplementedException();
}
public Task<Microsoft.AspNetCore.Identity.IdentityResult> UpdateAsync(AMEUserRole role, CancellationToken cancellationToken) {
throw new NotImplementedException();
}
#endregion
}
```
can anyone suggest me where it went wrong and how to fix this?
Could you include the exception stack trace?
Did you fixed? How?
Unfortunately there is not enough information here to reproduce the error.
Please include a more complete repro app, e.g. by uploading your app to GitHub so we can take a look. Also please provide the full exception details and stack trace so we can investigate. Thanks!
Ok, let me put this way: can we have our own DB schema for roles and identity and can use the existing asp.net IDENTITY framework, provided, we have to override the features. is it possible?
Sounds like a specific Identity question. You'd be better off filing a new issue on aspnet/Identity.
got it. i've created (implemented) IUser<> and UserStore
Please phanikvr how do you achive that am in a similar situation now
Hope you have restored the packages using package manager for all packages available in the solution. If it doesn't work, plz let explain me what and how are u trying to DI.
Thank you,
Venkat
Sent from my iPhone
On 24-Apr-2017, at 01:38, Moses Bassey Ekwere notifications@github.com wrote:
Please phanikvr how do you achive that
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub, or mute the thread.
Yes all microsoft identity packages has been restored.
Here is my code snippet for DI
services.AddIdentity
.AddUserStore
Where Account is my user properties information
public class AccountUserStore : Microsoft.AspNetCore.Identity.IUserStore
{
public Task
{
throw new NotImplementedException();
}
public Task<IdentityResult> CreateAsync(AccountUserRole role, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
public Task<IdentityResult> DeleteAsync(Account user, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
public Task<IdentityResult> DeleteAsync(AccountUserRole role, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
public void Dispose()
{
throw new NotImplementedException();
}
public Task<Account> FindByIdAsync(string userId, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
public Task<Account> FindByNameAsync(string normalizedUserName, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
public Task<string> GetNormalizedRoleNameAsync(AccountUserRole role, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
public Task<string> GetNormalizedUserNameAsync(Account user, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
public Task<string> GetRoleIdAsync(AccountUserRole role, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
public Task<string> GetRoleNameAsync(AccountUserRole role, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
public Task<string> GetUserIdAsync(Account user, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
public Task<string> GetUserNameAsync(Account user, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
public Task SetNormalizedRoleNameAsync(AccountUserRole role, string normalizedName, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
public Task SetNormalizedUserNameAsync(Account user, string normalizedName, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
public Task SetRoleNameAsync(AccountUserRole role, string roleName, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
public Task SetUserNameAsync(Account user, string userName, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
public Task<IdentityResult> UpdateAsync(Account user, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
public Task<IdentityResult> UpdateAsync(AccountUserRole role, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
Task<AccountUserRole> IRoleStore<AccountUserRole>.FindByIdAsync(string roleId, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
Task<AccountUserRole> IRoleStore<AccountUserRole>.FindByNameAsync(string normalizedRoleName, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
}
Here is the exception message am getting from the application.
InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.Identity.IRoleStore1[gtunes.Entity.AccountUserRole]' while attempting to activate 'Microsoft.AspNetCore.Identity.RoleManager
1[gtunes.Entity.AccountUserRole]'.
ive added the solution https://github.com/phanikvr/Custom_Identity_Core, plz look here and let me know whether it answers your questions/queries
@phanikvr That link leads to an empty repo with a readme. Do you have a solution you meant to push?
First, why not separate new store to 2 classes;
Secondly, my issue is in constructor. UserStore
It's wired, the 2 class should have other names to reduce confusion.