UserManager Keeps throwing a System.ArgumentNullException - c#

I am trying to implemented the standard Id in AspNetUsers from nvarchar to int. I've manage to get that side working. However my issue is when I try to login I keep getting an error from the UserManager class.
My code is below:
public class UserManager : UserManager<ApplicationUser, int>
{
public UserManager(IUserStore<ApplicationUser, int> store)
: base(store)
{
}
And on the login page I've got
if (IsValid)
{
// Validate the user password
var manager = Context.GetOwinContext().GetUserManager<UserManager>();
var user = manager.Find(UserName.Text, Password.Text); //This line throws the error
if (user != null)
{
IdentityHelper.SignIn(manager, user, isPersistent: false);
Response.Redirect("~/Home.aspx"); }
else
{
FailureText.Text = "Invalid username or password.";
ErrorMessage.Visible = true;
}
}
The error I keep getting is System.ArgumentNullException: Value cannot be null.
Parameter name: manager. Has anyone else come across this issue? Thanks in advance for your help
Stack Trace
[ArgumentNullException: Value cannot be null.
Parameter name: manager]
Microsoft.AspNet.Identity.UserManagerExtensions.Find(UserManager`2 manager, String userName, String password) +221
Account_Login.LogIn(Object sender, EventArgs e) in Login.aspx.cs:17
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9628026
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +103
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35
System.Web.UI.<ProcessRequestMainAsync>d__14.MoveNext() +5226

Make sure that UserManager is registered in you OWIN context. You should have something like this in your Startup class:
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
Also ensure that you have [assembly: OwinStartup(typeof(YourNamespace.Startup))] attribute applied to your web assembly.

the manager.Find() method only search in primary key of table, you must use something like :
Context db = new Context();
if (db.Users.Select(u => u).Where(u => u.Username == Username && u.Pass == Password).ToList().Count() == 1)
{
// login
}

I think the problem comes from the very basic of setups. Can you check that you're user manager is registered in your OWIN context.
And In any case you've extended the Identity ApplicationUser attributes, please do ensure that you've made them public and that they have getters and setters.

Related

Write a caught ASP.NET exception to EventLog without losing detail

This article explains in detail how to log an ASP.NET exception to the Windows EventLog and display a custom error page to the end-user.
However, the standard Event Logging mechanism for an ASP.NET web application automatically includes a lot of useful information not shown in this article. Implementing the code in the article results in a loss of detail/granularity in my error Event.
For example with automatic uncaught exception logging, you can see many attributes under the headings: Event Information, Application Information, Process Information, Request Information, Thread Information, Custom Event Details.
How can I implement logging of all of the same information that is logged in an uncaught exception and append my custom information into the section Custom Event Details? The best answer should preferably use some inbuilt method(s) of System.Diagnostics or System.Exception or similar i.e. writing as little code as possible to write the log entry with all sections mentioned above and simply append any custom details to the string.
If it is possible, I would also like to return the unique hashed Event ID (example b68b3934cbb0427e9497de40663c5225 from below) back to the application for display on my ErrorPage.aspx
Example of log format required:
Event code: 3005
Event message: An unhandled exception has occurred.
Event time: 15/07/2016 15:44:01
Event time (UTC): 15/07/2016 14:44:01
Event ID: b68b3934cbb0427e9497de40663c5225
Event sequence: 131
Event occurrence: 2
Event detail code: 0
Application information:
Application domain: /LM/W3SVC/3/ROOT-1-131130657267252632
Trust level: Full
Application Virtual Path: /
Application Path: C:\WWW\nobulus\nobulusPMM\Application\PMM\
Machine name: L-ADAM
Process information:
Process ID: 47216
Process name: iisexpress.exe
Account name: L-ADAM\Adam
Exception information:
Exception type: ApplicationException
Exception message: Error running stored procedure saveValidation: Procedure or function 'saveValidation' expects parameter '#ValidatedBy', which was not supplied.
at PMM.Models.PMM_DB.runStoredProcedure(String StoredProcedureName, List`1 SQLParameters) in C:\WWW\nobulus\nobulusPMM\Application\PMM\Models\PMM_DB.cs:line 104
at PMM.Models.PMM_DB.saveValidation(String PTLUniqueID, String ValidatedBy, DateTime ValidationDateTime, Nullable`1 ValidationCategoryID, String ValidationCategory, String Comment, Nullable`1 ClockStartDate, Nullable`1 ClockStopDate, String StartRTTStatus, String StopRTTStatus, String LastRTTStatus, Boolean MergedPathway, String MergedPathwayID, String ExtinctPathwayID, DataTable ChecklistResponses) in C:\WWW\nobulus\nobulusPMM\Application\PMM\Models\PMM_DB.cs:line 265
at PMM.Validate.lnkSaveButton_Click(Object sender, EventArgs e) in C:\WWW\nobulus\nobulusPMM\Application\PMM\Validate.aspx.cs:line 323
at System.Web.UI.WebControls.LinkButton.OnClick(EventArgs e)
at System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument)
at System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
Request information:
Request URL: http://localhost:6901/Validate?PTLUniqueID=RTT10487
Request path: /Validate
User host address: ::1
User: L-ADAM\Adam
Is authenticated: True
Authentication Type: Negotiate
Thread account name: L-ADAM\Adam
Thread information:
Thread ID: 19
Thread account name: L-ADAM\Adam
Is impersonating: False
Stack trace: at PMM.Models.PMM_DB.runStoredProcedure(String StoredProcedureName, List`1 SQLParameters) in C:\WWW\nobulus\nobulusPMM\Application\PMM\Models\PMM_DB. cs:line 104
at PMM.Models.PMM_DB.saveValidation(String PTLUniqueID, String ValidatedBy, DateTime ValidationDateTime, Nullable`1 ValidationCategoryID, String ValidationCategory, String Comment, Nullable`1 ClockStartDate, Nullable`1 ClockStopDate, String StartRTTStatus, String StopRTTStatus, String LastRTTStatus, Boolean MergedPathway, String MergedPathwayID, String ExtinctPathwayID, DataTable ChecklistResponses) in C:\WWW\nobulus\nobulusPMM\Application\PMM\Models\PMM_DB.cs:line 265
at PMM.Validate.lnkSaveButton_Click(Object sender, EventArgs e) in C:\WWW\nobulus\nobulusPMM\Application\PMM\Validate.aspx.cs:line 323
at System.Web.UI.WebControls.LinkButton.OnClick(EventArgs e)
at System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument)
at System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
Custom event details:
UPDATE
I actually found out using ILSpy and roaming in different framework classes that ASP.NET uses internally WebErrorEvent, which possess protected methods to achieve the same behavior.
Solution 1:
For that, just create a class which inherits WebErrorEvent, and then override its constructor:
public class CustomWebErrorEvent : WebErrorEvent
{
public CustomWebErrorEvent(string message, EventSource source, int eventCode, Exception ex) : base(message, source, eventCode, ex)
{
}
}
Then using it inside the Error management method from Global.asax:
protected void Application_Error(Object sender, EventArgs e)
{
// Log error to the Event Log
Exception myError = null;
if (HttpContext.Current.Server.GetLastError() != null)
{
var r = new CustomWebErrorEvent("error", null, 120, HttpContext.Current.Server.GetLastError());
}
}
I am pretty sure it is posslbe also to overload ASPNET to only raise straight a custom WebErrorEvent, but I didn't find it yet.
I am still trying to figure out how to add custom info to the event as overriding the method FormatCustomEventDetails is not getting called for Web Managed error events.
Solution 2:
If not possible as missing adding custom fields for now, you can use a similar method that I wrote which does the same output:
// Log error to the Event Log
Exception myError = null;
if (HttpContext.Current.Server.GetLastError() != null)
{
var request = HttpContext.Current.Request;
myError = HttpContext.Current.Server.GetLastError();
var dateAsBytes = System.Text.Encoding.UTF8.GetBytes(DateTime.Now.ToString("G"));
var id = Convert.ToBase64String(System.Security.Cryptography.MD5.Create().ComputeHash(dateAsBytes));
// Event info:
var eventMessage = myError.Message;
var currentTime = DateTime.Now.ToString("G");
var currentTimeUTC = DateTime.UtcNow.ToString("G");
// Application info:
var appDomainName = AppDomain.CurrentDomain.FriendlyName;
var appDomainTrustLevel = (AppDomain.CurrentDomain.IsFullyTrusted) ? "Full" : "Partial";
var appVirtualPath = VirtualPathUtility.GetDirectory(request.Path);
var appPath = request.PhysicalApplicationPath;
var machineName = Environment.MachineName;
// Process info:
var process = Process.GetCurrentProcess();
var processId = process.Id;
var processName = process.ProcessName;
var user = System.Security.Principal.WindowsIdentity.GetCurrent().User;
var accountName = user.Translate(typeof(System.Security.Principal.NTAccount));
// Exception info:
var exceptionType = myError.GetType().FullName;
var exceptionMessage = myError.Message;
var exceptionStack = myError.StackTrace;
// Request info:
var url = request.Url.AbsoluteUri;
var urlPath = request.Url.PathAndQuery;
var remoteAddress = request.UserHostAddress;
var userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
var isAuthenticated = HttpContext.Current.User.Identity.IsAuthenticated;
var authenticationType = System.Security.Principal.WindowsIdentity.GetCurrent().AuthenticationType;
// Thread info:
var impersonationLevel = System.Security.Principal.WindowsIdentity.GetCurrent().ImpersonationLevel;
var exceptionStack2 = myError.StackTrace;
// TODO: aggregate all info as string before writting to EventLog.
}
I found using existing .NET Apis almost all required fields from your output, just needed know to be aggregated as a string before outputting it in EventLog.
You can see that some of the object I am using (such as AppDomain.CurrentDomain, HttpContext.Current.Request or Process.GetCurrentProcess() returns a lot of other info, which could also ne added to the output if needed.
This all can be wrapped up under a method for code brevity of course.

Merging WCF webservice to my web application returns an exception

I would like to add an reference for WCF weather forecast webservice in my application.The thing is I have done everything correctly,but i am lacking somewhere else.It throws an Exception.The Exception message is in the bottom of this post.Somebody out there please help me to fix this.Unles or until fixing this i couln't move to the next phase.
My Source Code is here:
public partial class Testing : System.Web.UI.Page
{
weather.WeatherForecastServiceClient ws = new weather.WeatherForecastServiceClient();
protected void Page_Load(object sender, EventArgs e)
{
SortedDictionary<string, string> objDic = new SortedDictionary<string, string>();
foreach (CultureInfo ObjectCultureInfo in CultureInfo.GetCultures(CultureTypes.SpecificCultures))
{
RegionInfo objRegionInfo = new RegionInfo(ObjectCultureInfo.Name);
if (!objDic.ContainsKey(objRegionInfo.EnglishName))
{
objDic.Add(objRegionInfo.EnglishName, ObjectCultureInfo.Name);
}
}
foreach (KeyValuePair<string, string> val in objDic)
{
Country.Items.Add(new ListItem(val.Key, val.Value));
}
}
protected void callwebservice_Click(object sender, EventArgs e)
{
Result.Text = "Result" + ws.GetForecastByCity(City.SelectedItem.ToString(), Country.SelectedItem.ToString());
}
protected void Country_SelectedIndexChanged(object sender, EventArgs e)
{
string[] cities = ws.GetCitiesByCountry(Country.SelectedItem.ToString());
City.DataSource = cities;
City.DataBind();
}
}
This is my Exception
Server Error in '/' Application.
**The server was unable to process the request due to an internal error. For more information about the error, either turn on IncludeExceptionDetailInFaults (either from ServiceBehaviorAttribute or from the <serviceDebug> configuration behavior) on the server in order to send the exception information back to the client, or turn on tracing as per the Microsoft .NET Framework 3.0 SDK documentation and inspect the server trace logs.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ServiceModel.FaultException: The server was unable to process the request due to an internal error. For more information about the error, either turn on IncludeExceptionDetailInFaults (either from ServiceBehaviorAttribute or from the <serviceDebug> configuration behavior) on the server in order to send the exception information back to the client, or turn on tracing as per the Microsoft .NET Framework 3.0 SDK documentation and inspect the server trace logs.
Source Error:
Line 310:
Line 311: public testing.weather.Weather GetForecastByCity(string City, string Country) {
Line 312: return base.Channel.GetForecastByCity(City, Country);
Line 313: }
Line 314: }
Source File: E:\***\WeatherForecast\testing\Service References\weather\Reference.cs Line: 312
Stack Trace:
[FaultException: The server was unable to process the request due to an internal error. For more information about the error, either turn on IncludeExceptionDetailInFaults (either from ServiceBehaviorAttribute or from the <serviceDebug> configuration behavior) on the server in order to send the exception information back to the client, or turn on tracing as per the Microsoft .NET Framework 3.0 SDK documentation and inspect the server trace logs.]
System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg) +9442991
System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type) +345
testing.weather.IWeatherForecastService.GetForecastByCity(String City, String Country) +0
testing.weather.WeatherForecastServiceClient.GetForecastByCity(String City, String Country) in E:\sasi\WeatherForecast\testing\Service References\weather\Reference.cs:312
testing.Testing.callwebservice_Click(Object sender, EventArgs e) in E:\sasi\WeatherForecast\testing\Testing.aspx.cs:40
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +118
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +112
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.272**
You have implemented the service correctly. You can confirm this by running GetCitiesByCountry (it will return a list of cities). It is just that the method GetForecastByCity does not currently work.
Try any of these APIs instead.

LinqToTwitter OAuth Unauthorised

I'm following the article in the LinqToTwitter documentation:
http://linqtotwitter.codeplex.com/wikipage?title=Implementing%20OAuth%20for%20ASP.NET%20Web%20Forms&referringTitle=Learning%20to%20use%20OAuth
in order to allow users to log into my C# web application with Twitter.
Here's my code
private WebAuthorizer auth;
private TwitterContext twitterCtx;
protected void Page_Load(object sender, EventArgs e)
{
IOAuthCredentials credentials = new SessionStateCredentials();
if (credentials.ConsumerKey == null || credentials.ConsumerSecret == null)
{
credentials.ConsumerKey = ConfigurationManager.AppSettings["twitterConsumerKey"];
credentials.ConsumerSecret = ConfigurationManager.AppSettings["twitterConsumerSecret"];
}
auth = new WebAuthorizer
{
Credentials = credentials,
PerformRedirect = authUrl => Response.Redirect(authUrl)
};
if (!Page.IsPostBack)
{
auth.CompleteAuthorization(Request.Url);
}
twitterCtx = new TwitterContext(auth);
}
protected void AuthorizeTwitterButton_Click(object sender, EventArgs e)
{
auth.BeginAuthorization(Request.Url);
}
However I'm getting a Http 401 from the line:
auth.BeginAuthorization(Request.Url);
Here's the stack trace:
at LinqToTwitter.OAuthTwitter.WebResponseGet(HttpWebRequest webRequest)
at LinqToTwitter.OAuthTwitter.WebRequest(HttpMethod method, String url, String authHeader, IDictionary`2 postData)
at LinqToTwitter.OAuthTwitter.OAuthWebRequest(HttpMethod method, Request request, IDictionary`2 postData, String callback)
at LinqToTwitter.OAuthTwitter.AuthorizationLinkGet(String requestToken, String authorizeUrl, String callback, Boolean forceLogin, AuthAccessType authAccessToken)
at LinqToTwitter.WebAuthorizer.BeginAuthorization(Uri callback, Boolean forceLogin)
at LinqToTwitter.WebAuthorizer.BeginAuthorization(Uri callback)
at TwitterTest.Default.AuthorizeTwitterButton_Click(Object sender, EventArgs e) in c:\Users\agriffiths.SDASOL\Documents\Visual Studio 11\Projects\TwitterTest\TwitterTest\Default.aspx.cs:line 47
at System.Web.UI.WebControls.Button.OnClick(EventArgs e)
at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
The credentials have both the ConsumerKey and ConsumerSecret for my Twitter application set.
How can I authorise my users?
There are many reasons that Twitter API returns the HTTP 401 Unauthorized. I've put together a LINQ to Twitter FAQ that has an item on resolving 401's.
The LINQ to Twitter Downloads page has a WebFormsDemos project you can try and there's also a WebForms demo with the downloadable source code.

Problems in aspnet membership provider

I am using asp.net memebership
I log out the applcation by writting this code for log out
if (Membership.GetUser(HttpContext.Current.User.Identity).IsOnline) ;
{
FormsAuthentication.SignOut();
Response.Redirect(FormsAuthentication.LoginUrl);
}
Response.Redirect("loginpage.aspx");
After then I started the application and gettig the error in code block of authetication .below I am writtng a code and error message
else if (User.Identity.IsAuthenticated == true && Membership.GetUser( Membership.FindUsersByName(User.Identity.Name)).IsOnline==true)
{
FormsAuthentication.SignOut();
FormsAuthentication.RedirectToLoginPage();
MembershipUser currUser = null;
string gt = null;
if (HttpContext.Current.User != null)
{
currUser = Membership.GetUser(true);
gt = currUser.Email;
}
/// string temp = User.Identity.GetProfile().ID;
//string query = "Insert into user_tbl(MemberID)(" + temp + ")";
//Response.Redirect("UserPanel.aspx");
//return;
}
here Membership.FindUsersByName(User.Identity.Name)).IsOnline==true) I am getting this error
[ArgumentException: The provider user key supplied is invalid. It
must be of type System.Guid. Parameter name: providerUserKey]
I am unable to find what to do , I debug the application and found User.Identity.Name as a hash value
Stack Trace
Exception Details: System.ArgumentException: The provider user key supplied is invalid. It must be of type System.Guid.
Parameter name: providerUserKey
Source Error:
Line 86: }
Line 87:
Line 88: else if (User.Identity.IsAuthenticated == true && Membership.GetUser( Membership.FindUsersByName(User.Identity.Name)).IsOnline==true)
Line 89: {
Line 90:
Source File: d:\31 May 2012\Demo\LoginPage.aspx.cs Line: 88
Stack Trace:
[ArgumentException: The provider user key supplied is invalid. It must be of type System.Guid.
Parameter name: providerUserKey]
System.Web.Security.SqlMembershipProvider.GetUser(Object providerUserKey, Boolean userIsOnline) +2242996
System.Web.Security.Membership.GetUser(Object providerUserKey, Boolean userIsOnline) +40
System.Web.Security.Membership.GetUser(Object providerUserKey) +6
LoginPage.Page_Load(Object sender, EventArgs e) in d:\31 May 2012\Demo\LoginPage.aspx.cs:88
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +15
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +36
System.Web.UI.Control.OnLoad(EventArgs e) +92
System.Web.UI.Control.LoadRecursive() +54
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +772
Or you could also use Request.IsAuthenticated which dosent get the whole user object and just checks authentication.
According to Visual Studio, Membership.FindUsersByName returns MembershipUserCollection. You pass that object to the Membership.GetUser method, which does accept a parameter of the object type, but that object is supposed to be a user's ID (a GUID in ASP.NET). So, the exception is quite expected: you're trying to pass a collection of users, while the method expects a single user ID.
If you need to check whether a user is currently logged in, you can simply do this:
if (Membership.GetUser() != null)
See this MSDN Library article for more information on the method.

Connecting to new Facebook Client with access token

I finally got the right Facebook access token generated from the code. However now I have another problem. Using that access token to fetch user data. Every time I use the token to fetch data, it returns with an exception !String.IsNullOrEmpty(accessToken).
Full stack Trace:
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Diagnostics.Contracts.__ContractsRuntime+ContractException: Precondition failed: !string.IsNullOrEmpty(accessToken)
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[ContractException: Precondition failed: !string.IsNullOrEmpty(accessToken)]
Facebook.Web.FacebookWebClient..ctor(String accessToken) +137
Facebook_Photo_App._Default.Button1_Click(Object sender, EventArgs e) in Default.aspx.cs:105
System.EventHandler.Invoke(Object sender, EventArgs e) +0
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +118
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +112
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.1
My code:
To generate the access token on the page load:
string code;
code = Request.QueryString["code"];
//TextBox1.Text = code;
string token;
string url = "https://graph.facebook.com/oauth/access_token?client_id=XXX&redirect_uri=http://apps.facebook.com/dutch-vegas/&client_secret=XXX&code=" + code;
WebRequest request = WebRequest.Create(url);
WebResponse response = request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
token = reader.ReadToEnd();
string decodedtoken = token;
TextBox1.Text = decodedtoken;
decodedtoken = decodedtoken.Replace("access_token=", "");
int start = decodedtoken.IndexOf("&");
int count = decodedtoken.Length - decodedtoken.IndexOf("&");
decodedtoken = decodedtoken.Remove(start, count);
On the button implement the Access token to fetch user data
var client = new FacebookClient(decodedtoken);
dynamic me = client.Get("me");
string email = me.email;
string firstname = me.first_name;
string lastname = me.last_name;
string birthday = me.birthday;
TextBox1.Text = email;
Everything seems to be generating correctly. Even after getting the wrong access token (which was a lot shorter than it is suppose to be) for about a week, I got it sorted. And now I am stuck.
I have also noticed in the C# sdk for Facebook you can create multiple instances for FacebookWebClient, FacebookApp, FacebookClient.
Which one do I use and could this be part of my problem?
There are utility classes in the Facebook SDK that deal with the access token for you. They were a little flaky when I looked at them a few weeks ago though.
I ended up using the javascript code instead - http://www.patternwebsolutions.com/2011/03/07/facebooksdk-connect-mvc3/

Categories