我创建了用MVC托管的WCF项目,并希望实现WebSecurity.Login(string,string),如下所示
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;
}
}
我在App.Config(WCF项目是服务类)中有以下配置,但未在Web.Config(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" />
每次我在WCF测试客户端上测试时都会得到false,当我跟踪堆栈跟踪时,我得到了以下响应
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)
和
TargetSite {Void SetAuthCookie(System.String, Boolean, System.String)} System.Reflection.MethodBase {System.Reflection.RuntimeMethodInfo}
我已经花了好几个小时来弄清楚并看看 Why am I getting a NullReferenceException from Membership.GetCurrentUserName?以及 https://msdn.microsoft.com/en-us/library/ms731049.aspx
有人提出了一些有用的问题 Authentication to a WCF service not maintained using SetAuthCookie and a full .net client but works with silverlight
但无法解决这个问题。 如果您能提供一些代码片段和解释,请提前感谢您。