.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal
Post New Web Links

SPContext.Current is null In Custom Membership ValidateUser

Posted By:      Posted Date: September 29, 2010    Points: 0   Category :SharePoint

Hi,I just get a problem getting SPContext.Current in Custom membership Provider.

We have successfully plugin the custom membership into Sharepoint foundation 2010. We tried to programmically update the user's full name after user logs in and it failed in 2010 version. (good in 2007)

This is the simplified code. The class is a custom membership provider.Strangely, this only happens in 2010 version and never raised a problem in 2007.

public override bool ValidateUser(string username, string password)
   bool isvalid = false;

    if(username=="admin" && password="pwd")
      isvalid = true;
     var webContext = SPContext.Current.Web; //this returns null

   catch (System.Web.Services.Protocols.SoapException exp)
    Logger.WriteLog(exp.InnerException.ToString(), "wss login error");
   return isvalid;


Please Help. Thank you.



View Complete Post

More Related Resource Links

SPContext.Current is null in a WebService in the 12 hive layouts



  I do not understand why I cannot get the SPContext.Current instance.  It must be an IIS Setting because I get it on one machine and not on another, using identical code.

Custom Membership Provider - ValidateUser is not called



I am trying to implement a custom membership provider for forms based authentication under SharePoint foundation 2010.

In WSS3.0 everything works as expected but in SP2010 I find that whilst the Initialize method of my custom class is called ValidateUser is not??! I do get an error in the event log "An exception occurred when trying to issue security token. The security token username and password could not be validated". This is accompanied by an ASP.NET information log "Event code: 4006 Event message: Membership credential verification failed" 

Is there some change associated with "Claims authentication" that requires a different base class for this purpose now? 


Andrew Wiles - www.it-workplace.com - MDX made simple

SPContext.Current.Web.CurrentUser is null


I am using FBA in an internet zone of sharepoint site.

I am getting null in Current.Web.CurrentUser object even when i am logged in as FBA User. The welcome message is shown on top to ensure that FBA is working, but CurrentUser is giving null.

The same code works perfectly when logged in as Domain user in Intranet Zone.


SPContext.Current.Web.CurrentUser null in application page on 2008 r2 server

With my current situation I have:
a wss3 development environment on a 2003 R2 server
then a production 2008 r2 server.

For some reason on the 2008 server the SPContext.Current.Web.CurrentUser object is null for users who are site collection administrators but may not have permission to log into the server itself; however, on the 2003 R2 server the code works fine for those users.
When googling around, I had seen something about nt permissions on the bin folder sometimes being and issue until asp.net cache picks up the dll but this is a page in the _layouts directory and the code is in the .aspx page itself. It works fine for me as a system administrator on either machine.

Any suggestions? Thanks.

SPContext.Current returns a null


ok, I am new to the wonder world of WSS and Sharepoint.   The objective of my code is to upload a file into a particular folder into a document library.   Well, I was able accomplish this task ( ONCE ) on a Virtual machine but something happened somewhere and the code below no longer works:


public string UploadFile(string folderName, string fileName, byte[] contents)

{            String retVal = "File : " + fileName + " Upload Successfull.";


properties of SPContext.Current returns null after migration



I use stsadm export to migrate my site from one machine to another. After migration, I found one of my webpart does not work. It throws a null reference exception.

I logged my webpart and found it cannot get SPContext.Current.Web and SPContext.Current.List, the both returns null and cause the exception. I removed the webpart and added it again but the exception still occured. However, I created a new List on the new site then put my webpart in and it works fine.

I think it is because my migration operattion, is there anything need attention during migration?  Why SPContext.Current cannot get properties?

Users getting logged out because HttpContext.Current is null


We've  been trying to fix this one for a couple years, and I'm just out of ideas, so any help is really appreciated!

We've got a fairly large, complex web application that uses Forms Authentication to authenticate users.  Throughout the application we store and retrieve information about users in the Session object, and in some cases in cookies.  In a couple places we check for the existence of the context, and if it's null we send the user back to the login page to re-establish the session. 

Just a quick run-down of this web app: C#, .NET 3.5, IIS 6, ASP.NET State Service to manage session

As for the session timer, we use our own home-grown timer, which is basically a client-side timer, which is backed up by a check to the SQL database to see when their last activity was.  This seems to work well for us.  It's not perfect, but it allows us to notify the user before the session times out, and allows us to be certain we're not logging a user out before their 60 minutes of inactivity is up.

What's happening is that certain users are being logged out after just a few minutes.  We've eliminated the timer as a cause and believe what's happening is the HttpContext.Current is null, so the user is logged out.  We do not know why the HttpContext.Current is null, and I underst

Implementing a custom Membership Provider vs. using aspnet_Profile table - Which should I do?


I wanted to expand the amount of information that is associated with each user(MembershipUser) in the ASPNETDB.MDF database file. Adding columns like FirstName, LastName, etc. So I began implementing my own custom Membership Provider. I created a derived MembershipUser class with the extra private variables that I wanted each MembershipUser record to store. Its basically done. Almost after I did this I stumbled across the columns in the ASPNETDB.MDF->aspnet_Profile table called "PropertyNames" and "PropertyValuesString".  Could I have simply used these two columns to accomplish what I was attempting to do or are these columns for global application configuration settings?  

Is the aspnet_Profile table purpose to store expanded/related information about users such as personal information? Or is it for something else? Thanks.

Membership.ValidateUser "The serviceUri configuration setting was not found."


Hello everybody!

I use Membership.ValidateUser to check user credentials, but when I do so, I get the following ArgumentException: "The serviceUri configuration setting was not found." I just can't understand what is the problem here.

Here is a part from my Web.config:

      <membership defaultProvider="MembershipProvider">
            description="ASP.NET Membership database services."


Adding additional fields to my custom membership provider

Hi, i have been playing around with the membership provider model for the last week and have it got it working with a basic table schema.  The trouble i have is that i wish to add an additional field (eg First Name) but am not sure how i can do this.  Here's what i have so far: public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)    {        MembershipUser user = new MembershipUser(Name, username, providerUserKey, email, passwordQuestion, null, isApproved, false, DateTime.Now, DateTime.Now, DateTime.Now, DateTime.Now, DateTime.Now);        string sql = "INSERT INTO USERS(USERNAME,PASSWORD,EMAIL,ISACTIVE) VALUES(@UID,@PWD,@EMAIL,@ISACTIVE)";        db.AddParameter("@UID", username);        db.AddParameter("@PWD", password);        db.AddParameter("@EMAIL", email);        db.AddParameter("@ISACTIVE", (isApproved == true ? "Y" : "N"));        int i = db.ExecuteNonQuery(sql); 

Custom Dataflow componet in Null checking

Hi I want to create the custom Null checking and data length checking component. like Dataprofiling Please suggest it. Regards Ram  

Overriding web.config in sharepoint webpart/using custom membership in web part

I'm developing a web part for MOSS 2007. The web part is being deployed to a web application which utilizes forms-based authentication, and role/membership providers to control users. The membership table has been edited to include addition user info (I am aware, that this isn't the best way to handle additional user, i.e. profiles etc., however, this is a requirement from another department). Since my web part needs to access this information, I was thinking of implementing a custom membership provider. So far I've followed: http://msdn.microsoft.com/en-us/library/6tc47t75.aspx and I've got my custom membership class, and a very stripped down membershipprovider-class. But how do I enable/use this membership provider in my web part? I've tried creating a simple web.config in the "WebPartCode" folder of my solution, next to my webpart code file, with connection string and membership provider tags. But I don't even really know how to build/reference/use the membership provider itself, so I don't know what else I need to do. I've built a C# class library project with the two classes (membershipprovider and custom membershipuser) and compiled it and installed it in the global assembly cache - is this correct? and how do I access them from here? Hope someone can help

Get current logon user id with SPContext.Current.Web.CurrentUser.LoginName

Hi all,I've been trying to search for the method to display the currently logon user id. So far, most sites I've visited used SPContext.Current.Web.CurrentUser.LoginName.I've tried this in my SharePoint custom application page. Everything works great.I have a test account, when logged in, was displayed as xxx\testacc.For some reasons, I changed the id in AD to xxx\test and did a full import to MOSS.I logged in to the custom application with the changed id, i.e. xxx\test, BUT the value of SPContext.Current.Web.CurrentUser.LoginName still displayed as xxx\testacc!How do I get the latest id? Has anyone encountered this before?Thank you.Kelvin

Custom Membership Provider Problem

Hi there, I'm writing a web app using C# and .NET 2.0 (corporate environment, so I can't use 4.0) and I'm running into an issue with custom membership providers. Everything compiles, and looks like it should work, but I get this error when viewing the WSAT Security tab:  Could not load type 'BinCSF.Security.CUser' from assembly 'App_Code.f4qdas9h, Version=, Culture=neutral, PublicKeyToken=null' <membership> section of web.config: http://illogi.ca/l/snippets/bincsf_web_config.html BinCSF.Security.UserProvider: http://illogi.ca/l/snippets/bincsf_userprovider.html BinCSF.Security.CUser: http://illogi.ca/l/snippets/bincsf_cuser.html Any help would be greatly appreciated.

HttpContext.Current is null

I have an intermittent issue where HttpContext.Current is null.  I do have aspNetCompatibilityEnabled="True".  I can duplicate it by restarting the webservice and make the request.  The first request has the HttpContext.Current set to null but subsequent requests have the proper HttpContext.  I've done some tracing and the messages are exactly the same. What else is weird is that the HttpContext is NOT null in my global.asax but by the time it hits my custom UsernamePasswordValidator, it is set to null.   Any ideas on what i'm doing wrong? HttpRequest from tracing: <HttpRequest> <Method>POST</Method> <QueryString></QueryString> <WebHeaders> <Cache-Control>no-cache</Cache-Control> <Connection>Keep-Alive</Connection> <Content-Length>799</Content-Length> <Content-Type>text/xml; charset=utf-8</Content-Type> <Accept>*/*</Accept> <Accept-Encoding>identity</Accept-Encoding> <Host>mydomain.com</Host> <User-Agent>Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET4.0C; .NET4.0E; InfoPath.2)</User-Agent> <SOAPAction>"http://tempuri.org/ICategoryService/GetAvailablePickList"</SOAPAction> </WebHeaders> </Htt

Problem with Spcontext.current.web.currentuser.name

Hi all, I am using Spcontext in code to retrive the current user login name. But some times Spcontext.current.web.currentuser.name will return name of the user who is currently logged in it is retruning user name other than the current. When I refresh the page two or three times It will return the proper user name. Can anyone help me to resolve this issue.

current user identity in custom security trimmer

The crux of my problem is that I want to impersonate  a user’s NTLM credentials in the context of a SharePoint custom security trimmer to execute HttpWebRequests to check user access to URLs.  When accessing WindowsIdentity.GetCurent() in the security trimmer, the System.Security.Principal.WindowsIdentity object returns the identity of the application pool running the search query service, NOT the currently logged in user.   When accessing System.Threading.Thread.CurrentPrinciple.Identity in the security trimmer, the Microsoft.IdentityModel.Claims.ClaimsIdentity object of the current logged in user is returned.  However, there is a catch.  …   If I execute the following code in a .NET web application, the cast of the ClaimsIdentity to a WindowsIdentity succeeds because the identity has the authentication type of NTLM.    WindowsIdentity winId = (WindowsIdentity)System.Threading.Thread.CurrentPrincipal.Identity; WindowsImpersonationContext wic = winId.Impersonate(); request.Credentials = CredentialCache.DefaultCredentials; //access means a response comes back when a request is made to the url using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { returnStatus = true; } wic.Undo(); However, when I execute the same code in the context of the SharePoint security trimmer (the search query service li
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend