how to access session data from layout c# - c#

Good day,
I am trying to pass a user last name into my layout.
In my home index I get my user last name and assign it to my session.
var userId = User.Identity.GetUserId();
var usuerInDb = _user.GetUsuario(userId);
HttpContext.Session["LastName"] = usuerInDb .LastName;
//More code here ....
return view();
What I would like to do is to acess "LastName" from my session and assign it to my layout in some way like the following
//Layout
//....more code
Hello #session["LastName"]
How should I do it? I also accept other approaches.
Thanks

I would suggest using User Claims, to avoid bloating the session with a lot of user information.
https://korzh.com/blogs/net-tricks/aspnet-identity-store-user-data-in-claims

if are usin MVC you can use TempData["LastName"]
replace
HttpContext.Session["LastName"] = usuerInDb .LastName;
to
TempData["LastName"] = = usuerInDb .LastName;
In view..
Hello TempData["LastName"]

Related

Session after redirect ASP.net

I am pretty new to ASP.Net and I am testing how the sessions work after a redirect. The problem is that I am not able to retrieve my data after i redirected.
code form 1:
Session["person"] = person; // Object
Session["relation"] = relation; // Object
Response.Redirect("~/Fom2.aspx", false); // found online this has to be false to keep session token
code form 2:
Person person = (Person)(Session["person"]); //null
Relation relation = (Relation)(Session["relation"]); //null
Can anybody help me? Thnx
The string in Session["string"] has to be exactly the same. Remove the space from Session["person "] in form 2 so it is Session["person"]

Cache player with GameSparks and Unity

I am new to gamesparks, but so far I have set up a login/register function, which works like it should, but... How do I ensure that the user don't have to login next time he or she opens the app?
I found this in which I read that I can just run this:
GameSparkssApi.isAuthenticated().
First off all, in all other tutorials it states that it should be: GameSparks.Api.xxxx. Even when trying this I do not find isAuthenticated() anywhere.
GameSparks.Api.isAuthenticated();
I am hoping that someone can cast some light on this.
You can use Device Authentication for this purpose. This method of auth sets an access token for the device you are on. This token is stored and the client and gotten in the request. these requests are structured like so:
new GameSparks.Api.Requests.DeviceAuthenticationRequest()
.SetDeviceId(deviceId)
.SetDeviceModel(deviceModel)
.SetDeviceName(deviceName)
.SetDeviceOS(deviceOS)
.SetDeviceType(deviceType)
.SetDisplayName(displayName)
.SetOperatingSystem(operatingSystem)
.SetSegments(segments)
.Send((response) => {
string authToken = response.AuthToken;
string displayName = response.DisplayName;
bool? newPlayer = response.NewPlayer;
GSData scriptData = response.ScriptData;
var switchSummary = response.SwitchSummary;
string userId = response.UserId;
});
You can find more on this method in our documentation: https://api.gamesparks.net/#deviceauthenticationrequest
Regards Patrick, GameSparks.

Search DNN Portal User using custom Module

Hello to all programmers out there!
I am currently developing a module wherein I need to have a search for all users in my DNN portal. And I don't know what is the right way of doing so. I just have to populate a gridview with the users username and UserID with a field populated with a button. So overall. I need to have 3 fields wherein:
1st field = Username
2nd field = UserID
3rd Field = a Button(Which I already know how to include in a gridview)
I also have a textbox and a buttonSearch wherein in the textbox I will search for an existing username else, I shall throw an exception or message perhaps. So whenever there is a username existing prior to what I searched, the gridview will return with the information.
As of now I'm really puzzled here. And I only have this:
public static UserInfo GetUserByName(int portalId, string username)
{
}
And I'm not sure what to do next.
Any response would be really appreciated.
I updated my answer to reflect the function wrap that you originally suggested. I am using the GetUsersBasicSearch function of DotNetNuke.Entities.Users to look for a user by username. I'm not sure what you need a gridview for? If you are searching for a username, it will likely return just 1 user. Are you wanting to partially match the username and show multiple matches in a gridview?
public static UserInfo GetUserByName(int portalId, string username)
{
var foundUsers = UserController.Instance.GetUsersBasicSearch(portalId, 0, 10, "UserID", true, "UserName", username);
if (foundUsers.Any())
{
return foundUsers.FirstOrDefault();
}
else
{
return null;
}
}

How to store login user id in asp.net?

i am new to asp.net. my question is that how one can save login userid in asp.net webform?
code i am writing in asp.net webform is:
foreach (var s in db.Users)
{
if (tbUserName.Text==s.user_name && tbPassword.Text == s.user_password)
{
if (string.IsNullOrEmpty(Request.QueryString["ReturnUrl"]))
{
FormsAuthentication.SetAuthCookie(tbUserName.Text, false);
Response.Redirect("~/");
}
else
{
FormsAuthentication.RedirectFromLoginPage(tbUserName.Text, false);
}
flag = 1;
break;
}
else
flag=0;
}
if(flag==0)
{
tbUserName.ErrorText = "Invalid user";
tbUserName.IsValid = false;
}
}
As Tim said, you can get the authenticated user with
User.Identity.Name
You can also get the AuthenticationType and IsAuthenticated properties from the same object.
A suggestion would be to NOT query your DB for all of the users and then loop through them for the correct one. Based off of the user input, you should query the db for the one and only user which matches the form post.
Based off of what you wrote, it looks like the passwords are in clear text and not encrypted, which is a huge security issue. Being new to .Net, take a look at the .Net Membership Providers or SimpleMembership or a comparable pattern.
Good luck!
I would suggest you look at using the Session object to store the user ID. A Session will be available throughout that user's session on the site. Thus, you can call Session anywhere in your site's code to reference that user ID.
For example, to store the id, simply do this, pretend we're in Page_Load()
Session["UserId"] = userID // Or wherever you get the ID from.
then in your code behind, you can do this:
string userId = Session["UserId"]
If the user ID is a number, say an int, then you will need to cast the userID:
int userId = 0;
int.TryParse(Session["UserID"], out userID)
Quick dirty link to a Session example :
http://asp.net-tutorials.com/state/sessions/

Sitecore workflow approval/rejection emails

We are working on implementing some custom code on a workflow in a Sitecore 6.2 site. Our workflow currently looks something like the following:
Our goal is simple: email the submitter whether their content revision was approved or rejected in the "Awaiting Approval" step along with the comments that the reviewer made. To accomplish this we are adding an action under the "Approve" and "Reject" steps like so:
We are having two big issues in trying to write this code
There doesn't seem to be any easy way to determine which Command was chosen (the workaround would be to pass an argument in the action step but I'd much rather detect which was chosen)
I can't seem to get the comments within this workflow state (I can get them is the next state though)
For further context, here is the code that I have so far:
var contentItem = args.DataItem;
var contentDatabase = contentItem.Database;
var contentWorkflow = contentDatabase.WorkflowProvider.GetWorkflow(contentItem);
var contentHistory = contentWorkflow.GetHistory(contentItem);
//Get the workflow history so that we can email the last person in that chain.
if (contentHistory.Length > 0)
{
//contentWorkflow.GetCommands
var status = contentWorkflow.GetState(contentHistory[contentHistory.Length - 1].NewState);
//submitting user (string)
string lastUser = contentHistory[contentHistory.Length - 1].User;
//approve/reject comments
var message = contentHistory[contentHistory.Length - 1].Text;
//sitecore user (so we can get email address)
var submittingUser = sc.Security.Accounts.User.FromName(lastUser, false);
}
I ended up with the following code. I still see no good way to differentiate between commands but have instead implemented two separate classes (one for approve, one for reject):
public void Process(WorkflowPipelineArgs args)
{
//all variables get initialized
string contentPath = args.DataItem.Paths.ContentPath;
var contentItem = args.DataItem;
var contentWorkflow = contentItem.Database.WorkflowProvider.GetWorkflow(contentItem);
var contentHistory = contentWorkflow.GetHistory(contentItem);
var status = "Approved";
var subject = "Item approved in workflow: ";
var message = "The above item was approved in workflow.";
var comments = args.Comments;
//Get the workflow history so that we can email the last person in that chain.
if (contentHistory.Length > 0)
{
//submitting user (string)
string lastUser = contentHistory[contentHistory.Length - 1].User;
var submittingUser = Sitecore.Security.Accounts.User.FromName(lastUser, false);
//send email however you like (we use postmark, for example)
//submittingUser.Profile.Email
}
}
I have answered a very similar question.
Basically you need to get the Mail Workflow Action and then you need to further extend it to use the original's submitter's email.
Easiest way to get the command item itself is ProcessorItem.InnerItem.Parent
This will give you the GUID for commands like submit, reject etc.
args.CommandItem.ID
This will give you the GUID for states like Draft, approved etc.
args.CommandItem.ParentID

Categories