TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
osyris zosar
NA
289
26.8k
Creating Login System
Mar 14 2021 3:51 AM
I am trying to create my own login system but for some reason it wont add a new user to my MSSMS database
i have already created the migration and the tables are in the database
Startup Code:
public
void
ConfigureServices(IServiceCollection services)
{
services.AddIdentity<AppUser, AppRole>(options =>
{
options.User.RequireUniqueEmail =
true
;
}).AddEntityFrameworkStores<IdentityAppContext>()
.AddDefaultTokenProviders();
services.ConfigureApplicationCookie(config =>
{
config.Cookie.Name =
"Login_Cookie1"
;
config.LoginPath =
"/Account/login"
;
});
services.AddDbContext<IdentityAppContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString(
"Master"
)));
services.AddControllersWithViews();
}
appsettings.json:
"ConnectionStrings"
: {
"Master"
:
"Server=LAPTOP-VI29TVE0;Database=MasterDb;Trusted_Connection=True;MultipleActiveResultSets=true"
}
Controller:
public
class
AccountController : Controller
{
private
UserManager<AppUser> Usermgr { get; }
private
SignInManager<AppUser> SignInmgr { get; }
public
AccountController(UserManager<AppUser> userManager,
SignInManager<AppUser> signInManager)
{
Usermgr = userManager;
SignInmgr = signInManager;
}
public
async Task<IActionResult> Register()
{
var Person =
new
AppUser();
Person.UserName =
"username1"
;
Person.Email =
"
[email protected]
"
;
Person.FirstName =
"Firstname1"
;
Person.LastName =
"chees123"
;
IdentityResult result = await Usermgr.CreateAsync(Person,
"Password"
);
if
(result.Succeeded)
{
ViewBag.Message =
"Account was succesfully created"
;
}
else
{
ViewBag.Message =
"Something went wrong"
;
}
return
View();
}
}
IdentiyAppContext:
public
class
IdentityAppContext : IdentityDbContext<AppUser,AppRole,
int
>
{
public
IdentityAppContext(DbContextOptions<IdentityAppContext> options)
: base(options)
{
}
}
AppUser:
public
class
AppUser : IdentityUser<
int
>
{
public
string FirstName { get; set; }
public
string LastName { get; set; }
}
Approle is empty at the moment:
public
class
AppRole : IdentityRole<
int
>
//Identity calm
{
}
Register view page:
@{
ViewData[
"Title"
] =
"Register"
;
}
<h2>Register</h2>
<p>@ViewBag.Message</p>
<div>
<a asp-controller=
"Account"
asp-action=
"Login"
>Login</a>
</div>
it does not add to the database and i keep geting the message : "Something went wrong" (from the viewbag)
Reply
Answers (
2
)
C# function for below requirement.
Bootstrap modal is not appearing when on form save using ASP.NET MVC