2016-09-13 10 views
1

私はnginxサーバー内で実行されている角でSPAを開発しており、IISサーバーで実行されるASP.NET MVC4 Webアプリケーションは角度アプリにいくつかのAPI関数を提供します。ユーザーがログインするとすぐに、httpsessionにデータを保存する必要があります(角度のセッションストレージを使用しないと仮定します)。しかし、セッションは常にnullを返します。ログインしてもデータが保存されています。必要なコードのいくつかを見てください。Angular + ASP.NET MVC4セッション常にnull

authservice.js

login: function (email, password) { 
    var deferred = $q.defer(); 
    $http({ 
      url: global.API_URL + '/Auth/Login', 
      method: 'POST', 
       data: { email: email, password: encodeURIComponent(password)} 
    }).then(function (response) { 
     if (response.data.code == 0) { 
      //If success go to home 
}); 

AuthController.js

[ActionName("Login")] 
    [HttpPost] 
    public JsonResult Index(string email, string password) 
    { 
    CustResponse response = new CustResponse(); 
    //Get the logged in user data 
    User user = userService.GetUser(email); 

    //Get the hash 
    string hash = PasswordUtil.CreatePasswordHash(user.Salt, password); 

    //Custom membership provider 
    CustMembershipProvider provider = new CustMembershipProvider(); 
    bool isValid = provider.ValidateUser(email, hash); 

//Store the user token into the session 
    System.Web.HttpContext.Current.Session["TOKEN"] = //Some token value; 

    //if valid send response code 0 
    response.code = 0; 
    response.message = "Success"; 
    response.data = isValid ; 

    return Json(response); 
    } 

私はAuthControllerにいるとき、私はセッションオブジェクトでそれを見ることができるようにトークンがセッションに保存されつつあります。彼はホーム・ページにリダイレクトされていて、彼が以前に格納されたのいくつかをアクセスする必要がで

AwmsMembershipProvider.cs

public class CustMembershipProvider : MembershipProvider 
    { 
     public override bool ValidateUser(string email, string hash) 
     { 
      bool isValid = false; 
      using (var db = new AwmsContext()) 
      { 
       User user = db.Users.SingleOrDefault(a => a.Email == email); 
       if (user != null && user.PasswordHash != null &&   user.PasswordHash.Equals(hash)) 
       { 
        isValid = true; 
       } 
      } 
      return isValid; 
     } 

WebConfig.xmlは

<?xml version="1.0" encoding="utf-8"?> 
<!-- 
    For more information on how to configure your ASP.NET application, please visit 
    http://go.microsoft.com/fwlink/?LinkId=152368 
    --> 
<configuration> 
    <configSections> 
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> 
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> 
    </configSections> 
    <connectionStrings> 
    <add name="DefaultConnection" providerName="System.Data.SqlClient" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-AWMS-20160710224309;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-AWMS-20160710224309.mdf" /> 
    <add name="AwmsContext" connectionString="metadata=res://*/awms.csdl|res://*/awms.ssdl|res://*/awms.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=.\SQLEXPRESS;initial catalog=AWMS;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" /> 
    </connectionStrings> 
    <appSettings> 
    <add key="webpages:Version" value="2.0.0.0" /> 
    <add key="webpages:Enabled" value="false" /> 
    <add key="PreserveLoginUrl" value="true" /> 
    <add key="ClientValidationEnabled" value="true" /> 
    <add key="UnobtrusiveJavaScriptEnabled" value="true" /> 
    <add key="LogFilePath" value="./App_Data/Config/Log.xml" /> 
    </appSettings> 
    <system.web> 
    <httpRuntime targetFramework="4.5" /> 
    <compilation debug="true" targetFramework="4.5" /> 
    <authentication mode="Forms"> 
     <forms loginUrl="~/Account/Login" timeout="20" /> 
    </authentication>  
    <pages> 
     <namespaces> 
     <add namespace="System.Web.Helpers" /> 
     <add namespace="System.Web.Mvc" /> 
     <add namespace="System.Web.Mvc.Ajax" /> 
     <add namespace="System.Web.Mvc.Html" /> 
     <add namespace="System.Web.Optimization" /> 
     <add namespace="System.Web.Routing" /> 
     <add namespace="System.Web.WebPages" /> 
     </namespaces> 
    </pages> 
    <profile defaultProvider="DefaultProfileProvider"> 
     <providers> 
     <clear/> 
     <add name="DefaultProfileProvider" type="System.Web.Providers.DefaultProfileProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" applicationName="/" /> 
     </providers> 
    </profile> 
    <membership defaultProvider="AwmsMembershipProvider" userIsOnlineTimeWindow="15"> 
     <providers> 
     <clear /> 
     <add name="AwmsMembershipProvider" type="Awms.Dal.Provider.AwmsMembershipProvider" /> 
     </providers> 
    </membership> 
    <roleManager defaultProvider="EmblaRoleProvider"> 
     <providers> 
     <clear /> 
     <add name="EmblaRoleProvider" type="Awms.Dal.Provider.AwmsRoleProvider" /> 
     </providers> 
    </roleManager> 
    <!-- 
      If you are deploying to a cloud environment that has multiple web server instances, 
      you should change session state mode from "InProc" to "Custom". In addition, 
      change the connection string named "DefaultConnection" to connect to an instance 
      of SQL Server (including SQL Azure and SQL Compact) instead of to SQL Server Express. 
     --> 
    <sessionState mode="InProc" customProvider="DefaultSessionProvider" timeout="20" cookieless="false"> 
     <providers> 
     <add name="DefaultSessionProvider" type="System.Web.Providers.DefaultSessionStateProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" /> 
     </providers> 
    </sessionState> 
    </system.web> 
    <system.webServer> 
    <validation validateIntegratedModeConfiguration="false" /> 
     <httpProtocol> 
     <customHeaders> 
      <clear /> 
      <add name="Access-Control-Allow-Origin" value="http://localhost:8085" /> 
     </customHeaders> 
    </httpProtocol> 

    <handlers> 
     <remove name="ExtensionlessUrlHandler-Integrated-4.0" /> 
     <remove name="OPTIONSVerbHandler" /> 
     <remove name="TRACEVerbHandler" /> 
     <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" /> 
    </handlers></system.webServer> 
    <runtime> 
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> 
     <dependentAssembly> 
     <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" /> 
     <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" /> 
     </dependentAssembly> 
     <dependentAssembly> 
     <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" /> 
     <bindingRedirect oldVersion="1.0.0.0-4.0.0.0" newVersion="4.0.0.0" /> 
     </dependentAssembly> 
     <dependentAssembly> 
     <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" /> 
     <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" /> 
     </dependentAssembly> 
     <dependentAssembly> 
     <assemblyIdentity name="EntityFramework" publicKeyToken="b77a5c561934e089" /> 
     <bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="5.0.0.0" /> 
     </dependentAssembly> 
     <dependentAssembly> 
     <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" /> 
     <bindingRedirect oldVersion="0.0.0.0-1.3.0.0" newVersion="1.3.0.0" /> 
     </dependentAssembly> 
     <dependentAssembly> 
     <assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35" culture="neutral" /> 
     <bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" /> 
     </dependentAssembly> 
     <dependentAssembly> 
     <assemblyIdentity name="System.Net.Http.Formatting" publicKeyToken="31bf3856ad364e35" culture="neutral" /> 
     <bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" /> 
     </dependentAssembly> 
    </assemblyBinding> 
    </runtime> 
    <entityFramework> 
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" /> 
    </entityFramework> 
</configuration> 

ユーザーがログインすると別のコントローラからのセッション変数。

public JsonResult GetRoles() 
     { 
       String token = (String)System.Web.HttpContext.Current.Session["TOKEN"]; 
..... 
} 

私はいつもnullです。私はここで何か悪いことをしていますか?お気軽に貴重なアイデアを提供してください。

+0

以下の詳細を参照してください。 –

+0

申し訳ありませんが、それはそこにありましたが、フォーマットの問題のためにあなたに強調表示されない可能性があります。とにかく今すぐ利用可能です –

+0

web.configが正しく貼り付けられませんでしたか?それは正しく見えません。 – Amy

答えて

関連する問題