I have created WCF project hosted with MVC and would like to implement WebSecurity.Login(string,string) which is as follows
public bool LoginService(string UserName, string Password)
{
bool answer = false;
try
{
App.AppInitialize();
bool x = Membership.ValidateUser(UserName, Password);
Console.WriteLine(x.ToString());
if (x)
{
SetAuthCookie(UserName, false);
answer = true;
}
else
{
answer = false;
}
return answer;
}
catch (Exception e)
{
Console.WriteLine(e.StackTrace);
return answer;
}
}
And I have following config in App.Config(of WCF project which is service class) but not added in Web.Config(of MVC)
<system.web>
<!-- Configure the Sql Membership Provider -->
<authentication mode="Forms" />
<membership defaultProvider="SqlMembershipProvider" userIsOnlineTimeWindow="15">
<providers>
<clear />
<add
name="SqlMembershipProvider"
type="System.Web.Security.SqlMembershipProvider"
connectionStringName="ABCEntities"
applicationName="XXX.WCF"
enablePasswordRetrieval="false"
enablePasswordReset="true"
requiresQuestionAndAnswer="false"
requiresUniqueEmail="false"
passwordFormat="Hashed"/>
</providers>
</membership>
<compilation debug="true" />
Every time I test on WCF test client I get the false in return and when I track the stacktrace I get following response
StackTrace " at System.Web.Security.FormsAuthentication.SetAuthCookie(String userName, Boolean createPersistentCookie, String strCookiePath)\r\n at System.Web.Security.FormsAuthentication.SetAuthCookie(String userName, Boolean createPersistentCookie)
and
TargetSite {Void SetAuthCookie(System.String, Boolean, System.String)} System.Reflection.MethodBase {System.Reflection.RuntimeMethodInfo}
I have already spent hours and hours to figure it out and had a look at Why am I getting a NullReferenceException from Membership.GetCurrentUserName? and also in https://msdn.microsoft.com/en-us/library/ms731049.aspx
Little bit of helpful question was asked at Authentication to a WCF service not maintained using SetAuthCookie and a full .net client but works with silverlight
but was unable to sort it out. Thank you in advance if you could provide some code snippet along with explannation.