how to get userid in membership? - asp.net-membership

i know how to get userid for a current user
MembershipUser currentUser = Membership.GetUser();
Guid currentUserId = (Guid)currentUser.ProviderUserKey;
But for my application i need to get userid of user who are not loginned in also.
Because i need to assign the userid to different table which i m using.
Thank you in Advance

Use the Membership.GetUser(username, userIsOnline) method, passing false as the userIsOnline parameter.

Related

Go-Gorm: will the foreignkey be auto-populated when I set an object?

In the documentation we have this example:
type User struct {
gorm.Model
Name string
}
// `Profile` belongs to `User`, `UserID` is the foreign key
type Profile struct {
gorm.Model
UserID int
User User
Name string
}
If I do something like profile.User = &user, will that automatically populate the UserID field? Is it recommended to set both? Like:
profile.User = &user
profile.UserID = &user.ID
Or is that pointless? Furthermore, could I alternatively just set the UserID field and ignore the User field entirely?
If I do something like profile.User = &user, will that automatically populate the UserID field?
Just writing profile.User = &user will not populate the UserID field. Once you add the profile to the database. gorm will automatically populate the foreign key.
Is it recommended to set both?
Nope. In fact, you should not set the UserID yourself. This answers the last question as well.

Undefined property $id for users. But user has ID

I'm attempting to build up a query in Laravel.
I have two tables, with the following attirbuteds
User
id (auto)
name
email
userType
password
TenantPreferance
id (auto)
county
type
user_id (this is the user who created their
preference)
I'm trying to get a collection of data from users of a certain type where id of the user, matches the user_id in preferences.
But I get this error
Undefined property: Illuminate\Database\Eloquent\Builder::$id
$tenants = User::where('userType', 'tenant');
$Prefereances = TenantPreferance::where($tenants->id, $Prefereances->user_id);
$users = $Prefereances->get();
for : $tenants = User::where('userType', 'tenant');
must add:
first()
$tenants = User::where('userType', 'tenant')->first();
or : get()
$tenants = User::where('userType', 'tenant')->get();
You need to actually run the built sql. You also need to get a singular event of it out. So im using first() in this instance. Like this
$tenants = User::where('userType', 'tenant')->first();
$tentants = User::with('TenantPreferance')->where('userType', 'tenant')->get();
where TenantPreferance is the relationship name

converting from linq var result to another object

using the following LINQ query I want 'chosen' to be a 'User' object which I know and can manipulate, but it's not. How can I convert it to 'User'?
Thanks
Console.Write("User name:");
String nickname = Console.ReadLine();
var loggedin = from user in userList
where user.getNickname().Equals(nickname)
select user;
Currently, your query returns an IEnumerable<T> where T is the type of elements in userList.
It seems like you're after the FirstOrDefault eager operation to retrieve only a single User object if present.
var loggedin = (from user in userList
where user.getNickname().Equals(nickname)
select user).FirstOrDefault();
loggedin will be null if userList is empty, or if there is no element satisfying the provided predicate.
Using method syntax the same can be accomplished like this:
var loggedin = userList.FirstOrDefault(user => user.getNickname().Equals(nickname));

mvc3 formauthentication username to int value

I am using the formauthentication and as the user.identity.name I am using they're email, How can I change it to were I can use they're unique Registration ID instead?
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1,model.Email, DateTime.Now, cookieExpiration, true,model.Email);
The model.Email is the Email that the users have and its a String parameter I instead would like to put model.RegistrationID .
You can't change its data type but you can do what you want by keeping the RegistrationId in the database as string.
FormsAuthenticationTicket ticket= new FormsAuthenticationTicket(RegistrationId.ToString() ,model.Email, DateTime.Now,cookieExpiration, true,model.Email);

MVC3: Dispalying user name at front end, but controller must work user id

I hope i'm not bithering with my many questions...
i'm creating users in the ASPNETDB.MDF database with the asp.net configuration.
i have a client MVC, and i want when a client is created, edited, or deleted, it must dispaly the user_name and last_user_name of the user that performed the action. These two properties r in the client model. i want my controller to only deal with the ID(PK) of the users, and not their names. I only know how to make the controller work with the user names. How do i ensure that any functionality is done with the id but the display is the user_name? Here is my create method in my clientcontroller
[Authorize]
public ActionResult Create()
{
var model = new title
{
create_dt = DateTime.Now,
last_maint_dt = DateTime.Now,
row_version = 1,
status = "ACTIVE",
user_id = System.Web.HttpContext.Current.User.Identity.Name,
last_user_id = System.Web.HttpContext.Current.User.Identity.Name
};
return View(model);
}
If you are using the default ASP.NET Membership Provider you can find the user GUID for an account by accessing System.Web.Security.Membership. Then you can use the GetUser() method and pass it the username as an argument.
//Get Current User ID
var user = System.Web.Security.Membership.GetUser(User.Identity.Name);
if (user != null)
{
string id = user.ProviderUserKey.ToString();
}
And if you have the GUID and want the Username
var user = System.Web.Security.Membership.GetUser(id);
if (user != null)
{
string username = user.UserName;
}

Categories

Resources