alternatives to User.Identity.Name - asp.net

Afternoon all,
Im am displaying the username on a web page to state who has locked the web page for editing. I am using the following code...
If String.IsNullOrEmpty(lock.LockedBy) Then
lock.LockedBy = User.Identity.Name
hdnIsLockedBy.Value = User.Identity.Name
lock.AgendaID = Integer.Parse(lblAgendaNumber.Text)
End If
I understand that User.Identity.Name brings back the Domain\Name. I was wondering if i can just pull back the name of the user only as i cant see any suitable alternatives?
Regards
Betty

If you can assume that the domain name will be the same for all users, simply strip it from User.Identity.Name using String.Replace
User.Identity.Name.Replace("MyDomainName\", "")

I'd just do something like this:
lock.LockedBy = User.Identity.Name.Contains("\\")? User.Identity.Name.Substring(name.IndexOf("\\")+1):User.Identity.Name;
If it's something you are doing often, move it to a function.

Related

how to remove data from an application variable

i wanted to add to my website the option of seeing the usernames of the people that are logged in my website as a list.
i did it using the Application state so when a person loges in the application variable adds it to itself.
but when a person loges out of the website i need to remove his username from the list and i'm having trouble with this... any code suggestions?
in the Login page:
Application["UserList"] += Session["UserName"].ToString() + "<br/>";
and i tried this in the Logout page but it didnt work...:
String name = Session["UserName"].ToString();
Application.Remove(name);
you would use something along the lines of this:
Application["UserList"].ToString.Replace(Session["UserName"].ToString() + "<br/>", "");
But you really should not try manage a string. Use something like a HashTable and store that object in the application state. It's far easier to manage key/value pairs in a dictionary type construct.
Also it separates the data from the formatting which gives you much greater control over the output.

Pass parameter from 1 page to another using post method

I have a page "Demo.aspx". I need to set some parameters using post method and redirect the page to "DemoTest.aspx".
Is there any way to set parameters in post method in asp.net? I don't want to set "Querystring" due to security propose.
Also I need server side code for this. I am not able to use "Javascript" or "Jquery" for the same.
Here is some more description for the same.
Right now I am using Response.Redirect("www.ABC.Com/DemoTest.aspx?P1=2"). So the page is simply redirect to the given URL.
Now I don't want to pass that "P1" in "Querystring". Instead of query string I want to use Post method.
Please note that redirection page is not in my own application. So I cant maintain session or "Viewstate".
Thanks in advance.
Use a session variable and response.redirect to the next page.
Session["MyVariable"] = "someThing";
Response.Redirect("DemoTest.aspx");
The value stored in Session variables will be accessible across application.
you can store in session like this :
Session["id"] = "anyID";
To get values On another page you need to write
string id = Convert.ToString(Session["Id"]);
However
By default, in .NET pages post() do the things automatically.
You will need to do sumthing like this:
Server.Transfer("DemoTest.aspx", True)

Use session to hold data

I have 3 text boxes where user will enter some value inside it. Then user will click on done button which then it will redirect user to another page and the page will show user what data entered by them before in label before they hit save button to be saved in db. I would like to use session in this case but the i dont know how. Can someone tell me how? Thank you.
Have a look at
ASP.NET Session State Overview
How to: Save Values in Session State
How to: Read Values from Session State
I would like to use session in this case but the i dont know how.
Checkout the following article on MSDN which talks about the ASP.NET Session. To store something into the session you could use the following:
Session["Key1"] = TextBox1.Text;
    Session["Key2"] = TextBox2.Text;
Session["Key3"] = TextBox3.Text;
and then when you want to read the values back:
string value1 = (string)Session["key1"];
string value2 = (string)Session["key2"];
string value3 = (string)Session["key3"];

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

How can I get CURRENT USERNAME in membership asp.net 2008

I use a membership in asp.net 2008. And I build a login system with some rolls ...etc
I have a database and tables and the KEY is "username", and now I want to know how can I get a username for the member who logged in ?
for example:
I logged in as TURKI
I want to get the username TURKI
really I need for help...
thanks,
User.Identity.Name
User is a property of the page. If you need to access it elsewhere, you can use:
HttpContext.Current.User.Identity.Name
A modification to Freddy's answer when using MVC - had to use:
HttpContext.User.Identity.Name
Slight change but figured I would post it in case anyone else trying to do this in MVC hits the same snag.
Context.User.Identity.Name
you can use the Membership.GetUser() method described here:
http://msdn.microsoft.com/en-us/library/system.web.security.membership.getuser.aspx
also, if you do get the MembershipUser you could also get the 'ProviderUserKey' which uniquely identifies a user, possibly using that as your FK for your tables, that way your user can have their username updated without having to change all the keys in your tables.

Resources