ASP.NET Core Identity - how to use? - asp.net

I want to find the user using identity.name, but if there are several users with this name, go to error.
I want to return one user using cookies, email is unique, but name not, how to use email?
enter image description here
enter image description here
enter image description here
enter image description here

As per my understanding you have something like this;
You have multiple users with the same User.Identity.Name --> How is it possible to have multiple users with the same name?
And you want to use email address instead User.Identity.Name --> If you pass email from the view to controller action, you can use something like below;
var user = await UserManager.FindByNameAsync(model.Email);

Related

How to set expiration time for a link sent via Email?

I send a link as email to the users asking them to reset the password. Clicking on the link redirects to a page in my project. How can I set the expiration time for this link to 24 hours, and how do I know if this link is not re-used again? Whats the best way to do this?
Thanks
I dont know how your process looks like but the recommend way is to use a guid to identify the
passwort reset process.
This is how a process should look like.
Create a database table with the userId, createDate, closeDate, and a guid
Create a new entry in the table
Send the mail with a link to your page that has the uuid from the entry
If the user enters the page (clicks the link) you check if the process is still open (closeDate is null)
Check if the createDate is within the last 24 hours
User can change password
You set the closeDate
store email sent date&time in the database(where your maintaning userdetails) .
when your are open link for reset password capture present date. compare both date&time values . they you can procced. may this helps you.
I have an idea
Create a Time.now() and encrypt this using encryption and attach with your link
Send using Email and other option
go to the link page
write code on page load Request the sent encrypted Date Time and decipher it
match time if time is over then send to an error page else continue
Like -:- http://Yourhost/foldername.aspx?val=encryptedTimeDate

Change Umbraco member password with out knowing old password

I need a functionality to change umbraco member password programatically.user can add their new password in the field I had set on umbraco node.and when they publish the node new password will come in effect.I had find a way to change current password to given one
member.ChangePassword(oldPassword, password);
But this requires oldpassword to work.and I cant provide it as user has already changed old password in the umbraco node.then I tried to get old password programatically.
string theUserPassword = Membership.GetUser(username).GetPassword();
but this also throws an error
Password Retrieval Not Enabled.
Is there any way to get old password programatically?Or Am I going in the wrong direction?
Umbraco uses the Microsoft Membership Provider.
You probably have set the property "EnablePasswordRetrieval" to false.
If you don't know the password but need to change it, you can reset the password bij using the ResetPassword method.
I know this is an old post and an answer has already been accepted, but you can actually achieve what the OP wants to do by using the return value of the ResetPassword method for the oldValue parameter of the ChangePassword method:
member.ChangePassword(member.ResetPassword(), "New Password")
This allows you to change the password for a user to a specific value without knowing their existing password.
Another option to an old question:
I am using Umbraco 7.2.4 and here is how I change the password.
var member = Services.MemberService.GetByUsername("username");
Services.MemberService.SavePassword(member, "new password");
Where "Services.MemberService" is from ApplicationContext.Current.Services.MemberService.
The first line of code is where you get the member for which you want to change the password. This can also be done by email or id.
Services.MemberService.GetByEmail("email")
Services.MemberService.GetById(id)
The second line is where you change the password. It is automatically hashed.

Edit Email with Dynamic fields

My website has automatic email to users and there is some dynamic fields in email text. I mean, it automatically extract name family and prefix of each user and send it them.
for example:
dear prefix name family: ->
dear Dr John Smith:
you account is active.
Now i want to add the capability of editing this email context to admin part. The problem is some fields are dynamic. my code is like this:
bodyMsg.Append( " Dear "+prefix+name+family+", thank you for accepting our request.Your username is your email and here is your Password :" + password);
As you see, name, family, prefix and password are dynamically change. Now admin needs to edit email context. He should do that from his control panel in a Rad Editor. For example change the place of name or family in email context.
What i need is when admin used key words such as prefix, name or family the system detect the dynamic fields. and the email edited with dynamic fields place.
can any body help me?
thanks
Last time I had to do something similar, I let them use predefined template-words in their text.
"Dear {prefix} {name} {family}, thank you for accepting....".
Afterwards I replaced the template-words in the text with the values.
EDIT
We do this stuff with mailtemplates that needs to be sent by an app.
The templates are created by someone and stored in the database.
They've got documentation with a list of dynamic fields they can use in the mailtemplate, like in the example below: {ReplaceWithName}.
In my app I'll replace those dynamic fields with the appropriate data:
Person person = GetPersonInfo(); //Gets the information of the person
string body = GetBodyFromDatabase(); //Retreives the created mailtemplate
body = body.Replace("{ReplaceWithName}", (person.Firstname + " " + person.Lastname)); //Do this for each dynamic field
mailMessage.Body = body;
This will of course put some responsibility to the user that creates those templates.

ASP.NET Web Pages - Change Websecurity.currentUserName value

I am building a simple web page in ASP.NET Web Pages 2 (Razor). I have used the StarterSite template, which, by default has a registration / login system. By default you can't have a username, just an email address and password. I altered the "UserProfile" table in the database to be able to add a username for those who'd like to.
When logged in, it greets me like: "Hello, email-address" where email-address is the address you are logged in with.
In code it looks like this: "Hello, #Websecurity.currentUserName!"
Basically, what I'd like to do is that the CurrentUserName property instead of getting the CurrentUsername as the email address from the DB, get the UserName Column's value.
This is my UserProfile table structure:
Email - nVarchar(4000)
ID - int
UserName - nVarchar(4000)
How do I use this?
I managed to solve it.
You need to change the WebSecurity.InitializeDatabaseConnection method in your AppStart file like this:
WebSecurity.InitializeDatabaseConnection("Your DB Name", "Your Table Name", "What Column you want for the CurrentUserId as value", "What column you want for the CurrentUserName as value", true);
Hope this helps someone

asp.net membership custom change of passwords by users and admins

I am still searching StackOverFlow as well as the innertubes but have not found an example of what I need to do.
If an user has forgotten their password and they correctly answer their reminder question, the user is shown two form fields for entering a new password; Not Emailing or displaying a random generated password.
If the user calls the support center, an admin can change the password, the reminder question and the answer.
Thanks,
James
just use this code:
var user = Membership.GetUser(username);
user.ChangePassword(user.ResetPassword(), newPassword);
this simply first resets the password and then changes it to the new password, you don't need to know the reseted intermediate password

Resources