Web Api - User.Identity.Name и User.Identity.GetUser.Id () возвращает ноль в среде хостинга - PullRequest
0 голосов
/ 02 июня 2019

Web Api 2 - User.Identity.Name и User.Identity.GetUser.Id () возвращает ноль в среде хостинга, но отлично работает при локальном запуске.

private ApplicationUserManager _userManager;

    public ApplicationUserManager UserManager
    {
        get
        {
            return _userManager ?? Request.GetOwinContext().GetUserManager<ApplicationUserManager>();
        }
        private set
        {
            _userManager = value;
        }
    }
    public IdentityUser CurrentUser => UserManager.FindByName(User.Identity.Name);
    ApplicationRoleManager RoleManager => Request.GetOwinContext().GetUserManager<ApplicationRoleManager>();

    // Usage:
    var uid = CurrentUser.UserName; // Returns Null

Так как он работает на локальном хосте и не работает на хостинговой среде, я подозреваю некоторую проблему с web.config:

	<appSettings>
		<add key="webpages:Version" value="3.0.0.0" />
		<add key="webpages:Enabled" value="false" />
		<add key="ClientValidationEnabled" value="true" />
		<add key="UnobtrusiveJavaScriptEnabled" value="true" />
	</appSettings>
	<system.web>
		<authentication mode="None" />
		<customErrors mode="Off" defaultRedirect="/Shared/Error">
			<error statusCode="404" redirect="/Shared/Error404" />
		</customErrors>
		<compilation debug="true" defaultLanguage="c#" optimizeCompilations="true" targetFramework="4.6.1" />
		<httpRuntime targetFramework="4.6.1" maxRequestLength="51200" executionTimeout="300" maxQueryStringLength="32768" maxUrlLength="65536" />
	</system.web>
	<system.webServer>
		<!-- attribute runAllManagedModulesForAllRequests and remove entry "WebDAVModule" was required for PUT request to work-->
		<modules runAllManagedModulesForAllRequests="true">
			<remove name="FormsAuthentication" />
			<remove name="ApplicationInsightsWebTracking" />
			<remove name="WebDAVModule" />
		</modules>
		<handlers>
			<!-- This entry was needed for PUT request to work -->
			<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
			<remove name="OPTIONSVerbHandler" />
			<remove name="TRACEVerbHandler" />
			<remove name="WebDAV" />

			<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
			<!--<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />-->
		</handlers>
		<httpProtocol>
			<customHeaders>
				<!--
				<add name="Access-Control-Allow-Origin" value="*" />
				<add name="Access-Control-Allow-Headers" value="Origin, X-Requested-With, Content-Type, Accept,Authorization" />
				<add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
				-->
			</customHeaders>
		</httpProtocol>
		<validation validateIntegratedModeConfiguration="false" />
	</system.webServer>
  
...