ASP.NET Web Pages - Change Websecurity.currentUserName value - asp.net

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

Related

ASP.NET Core Identity - how to use?

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);

Show Logged In User Name in redirect.aspx Page

I'm a complete stranger to ASP.NET. But, I've had a project to do using it & faced a problem.
It is :
I have a login.aspx File - Where Users provide login User name & Password
If Login details (match Data Base) OK then User automatically redirects to logged_in.aspx.
There's a label (lbl_show) in redirected logged_in.aspx.
I need to show Logged in Username in it.
I read bunch of articles & came with nothing because of my lack of understanding so please help me.
se session variables in order to pass any value from one page to another.
Assign the Username value to the session variable and use it in your logged_in page as follows:
// In login page
Session["UserName"] = txtUserName.text;
//In logged_in page
label1.text = Session["UserName"];
Also refer the following link for State Management:
http://www.codeproject.com/Articles/492397/State-Management-in-ASP-NET-Introduction
You need to set an Authentication Cookie. It's easy and will allow you to leverage ASP.NET functionality easily (many built-in controls and also user-access control). I detail how in this SO post:
Using cookies to auto-login a user in asp.net (custom login)
The problem with the code
// In login page
Session["UserName"] = txtUserName.text;
//In logged_in page
label1.text = Session["UserName"];
Is casting is missing it should be
label1.text = Session["UserName"].ToString();
Edit 1
As Session contains object and if you have something other than object then you will have to explicitly cast it in your require type.
Suppose you have array in you Session then you will have to cast it back to array.
String[] Names={"abc","def","ghi"};
Session["NamesCol"]=Names;
Then if you want to use it you will have to cast it as follow
String[] NewNames=(string[])Session["NamesCol"];

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 / VB.NET Check If a (different) User IsInRole

I have an ASP.NET application on our company's intranet. And a funky security requirement.
I need to check to see if a given username is in a certain role. I cannot use
Page.User.IsInRole("MyDomain\MyGroup")
because
Page.User.Identity.Name
Returns an empty string. Because of some lovely specifications for this program, I have to keep anonymous access enabled in IIS. Seems to rule out any page.user.identity stuff.
So I did find a way to (at least) get the current user (from System.Environment.UserName), but I need to bounce it against the domain group to see if they're in it. Or, better yet, get a list of users within a given domain so I can check myself. Something like...
Dim UserName as String
UserName = System.Environment.UserName
If User(UserName).IsInRole("MyDomain\MyGroup") Then
MyFunction = "Success"
End If
-OR -
Dim GroupUsers as String()
GroupUsers = GetDomainUserNames("MyDomain\MyGroup")
Anybody have any ideas?
You can call IsUserInRole from the Roles static class. Here is a sample and some reference materials.
Roles.IsUserInRole(username, rolename);
link: http://msdn.microsoft.com/en-us/library/system.web.security.roleprovider.isuserinrole.aspx

Resources