I have a web Api in C#. I need user id/name in the api controller function when ever the user tries to access the api.
I tried Request.Principle and Http.Context.User but in both the identity.Name is comming as null. Can you please tell how can I get the username in the Api.
string _userName = System.Web.HttpContext.Current.User.Identity.Name;
Try this one.
You can use the following in you class to get the user Id
var userId = System.Web.HttpContext.Current.User.Identity.GetUserId();
Related
I've implemented social authentication in my application, and I get the accesstoken successfully.
But when I use that access token to get the user's public profile, I get only the user's ID and the user's name.
I can't get the other properties of the user like the user's profile picture.
when I try this:
Json = await httpClient.GetStringAsync($"https://graph.facebook.com/v5.0/{userWithOnlyId.Id}?access_token={accessToken}");
I get only the user's id and name.
when I do this as stated in this facebook doc :
await httpClient.GetStringAsync(
$"https://graph.facebook.com/me?fields=profile_pic&access_token={accessToken}");
I get a 403 forbidden error.
Is there something I did wrong ? I've tried several things I found here on stackoverflow but up till now I have no solution.
There is an example(section: Retrieving a Person's Profile):
https://graph.facebook.com/<PSID>?fields=first_name,last_name,profile_pic&access_token=<PAGE_ACCESS_TOKEN>
Looks like you need to provide a PSID
ASID(App-scoped ID) - it's a user-id that is unique for each app.
It means that this user will have different ASID for each app. I think that you have only one app.
To get a Page-scoped ID from an App-scoped ID, send a POST request to
the /pages_id_mapping root node
I dont do aspx at all so trying to work out this simple task.
I see the following code in some cs files which guess gets the current user and i assume this is a standard method in asp but might be wrong:
CS:
User user = (User)Context.Items["CurrentUser"];
I have tried things like this from other posts on here but maybe this system is different or the setup is different? again i dont know.
var currentUser = Membership.GetUser(User.Identity.Name);
string username = currentUser.UserName; //** get UserName
Guid userID = currentUser.ProviderUserKey; //** get user ID
Does anyone know how i can get the Name and User ID of the current user based on what is written above?
it depends on how you handle users in your website.
if you use the asp.net built in user management, then User.Identity.Name will get you the currently logged in username.
other stuff like (User)Context.Items["CurrentUser"] or (User)Session["myUser"] will get you the user which was saved in those places somewhere in your website.
you just need to start your way from the login page, and follow the functions to see how users are being handled in your website.
I have a facebook application with has to post updates on specific facebook page. The application has rights to post updates on the facebook wall of the page's admin. But how can i get the (admin's) users access token, which i need to get the access token of the page. Do i have to save the access tokenthe first time the user allows the app to post (offline access)?
Thanks for your reply!
I use the Facebook C# SDK as well. You'll need the following permissions:
manage_pages
read_stream
publish_stream
HTTP Get to "me/accounts" and get the .data object off of the returned value. This will be a list of the following:
var list = ApiGet("me/accounts").data;
foreach (dynamic acc in list)
{
var name = (string)acc.name;
var accessToken = (string)acc.access_token;
var category = (string)acc.category;
var pageId = (string)acc.id;
}
As you can see, each one of the pages have their own unique access token.
I need help on oAuth authentication to facebook. I am able to do the steps that facebook API mentioned and successfully authenticated, get my profile information as well as post to my news feed.
I want my application to login automatically rather than routing to Facebook login page on behalf of a user(i.e. get credentials from db and do auto oauth) . So that the user can type a message on application and click on button should submit a post to facebook page.
Thanks
What you need to get is an access token. This is a key which allows you continued access to the account until your the token expires or the user cancels it.
When you first authenticate if you have set the required permissions you get an authtoken which you can store and use to initiale further calls.
var client = new FacebookClient("my_access_token");
dynamic result = client.Get("19292868552_118464504835613");
string id = result.id;
string fromName = result.from.name;
string fromCategory = result.from.category;
string message = result.message;
int likes = result.likes;
foreach (dynamic comment in result.comments.data) {
string commentId = comment.id;
string commentMessage = comment.message;
}
see this article for details about the process:
http://benbiddington.wordpress.com/2010/04/23/facebook-graph-api-getting-access-tokens/
When someone logs on to my site. I want to direct them to their own home page. If the user has the id of 1. They would go to
http://www.test.com/Home.aspx?id=1
I already have the login and id setup. I am not sure how to incorporate it into the url.
Response.Redirect("http://www.test.com/Home.aspx?id=" + id);
Are you using Forms Authentication?
If so, instead of using RedirectFromLoginPage (which will redirect to whatever page is in your web.config), just use FormsAuthentication.SetAuthCookie, and do your own redirection.
To do so, you need to make use of the URL QueryString.
E.g
// forms auth code here, user is logged in.
int id = 1;
string redirectUrlFormat = "http://www.test.com/Home.aspx{0}";
string queryStringidFormat = "?id={0}";
Response.Redirect(string.Format(redirectUrlFormat, string.Format(queryStringidFormat, id)));
You should handle all querystring parameters, URL, etc (ie the above code) in a global static model class.
That way you can just say:
Response.Redirect(SomeStaticClass.GetUserHomePageUrl(id));
In the receiving page (Home.aspx), use the following code to get the id of the user:
var userId = Request.QueryString["id"]; // again, this "magic string" should be in a static class.
Hope that helps.