我开始为移动应用程序构建一个web api,我很难实现身份验证.我使用Bearer,虽然一切都应该没问题,但我无法让当前用户从控制器中获取动作.HttpContext.Current.User.Identity.Name为null(同样是HttpContext.Current.User.Identity.GetUserId())的结果.以下是重要代码:
Startup.cs:
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
var config = new HttpConfiguration();
ConfigureAuth(app);
WebApiConfig.Register(config);
app.UseWebApi(config);
}
}
Run Code Online (Sandbox Code Playgroud)
Startup.Auth.cs
public partial class Startup
{
static Startup()
{
OAuthOptions = new OAuthAuthorizationServerOptions
{
TokenEndpointPath = new PathString("/token"),
Provider = new ApplicationOAuthProvider(),
AccessTokenExpireTimeSpan = TimeSpan.FromDays(1),
AllowInsecureHttp = true
};
OAuthBearerOptions = new OAuthBearerAuthenticationOptions();
}
public static OAuthAuthorizationServerOptions OAuthOptions { get; private set; }
public static OAuthBearerAuthenticationOptions OAuthBearerOptions { get; private set; }
public static string PublicClientId { …Run Code Online (Sandbox Code Playgroud) 我有一个在 4.0 框架上运行的 ASP.NET Web 窗体应用程序项目。我对 Web 应用程序非常熟悉,但对 Web 窗体应用程序不太熟悉。今天我遇到了他们两个之间的显着差异,我很困惑为什么这种差异甚至存在。
通常,在 Web 应用程序中,如果我想确定页面的当前用户是谁,那么我只需从User.Identity.Name检索他们的域登录信息。因此,当我在调试环境中按F5 运行我的解决方案(我的 Web窗体应用程序!)并发现 User.Identity.Name 为空时,我感到非常困惑。所以我然后在谷歌上搜索“User.Identity.Name 是空的”并遇到了一堵链接墙,这些链接主要是在他们没有禁用 IIS 中的匿名身份验证时遇到这个问题的人。当然,这不是我的问题(我认为),因为我只是在此处从 Visual Studio 2012 进行调试。
但是,我继续挖掘,最终在 HttpContext 上发现了一些不为空并返回用户域登录信息的晦涩属性。该属性是HttpContext.Current.Request.LogonUserIdentity.Name。这让我想到了我的问题......
问题:在我的 Web 窗体应用程序中,为什么 User.Identity.Name 为空而 HttpContext.Current.Request.LogonUserIdentity.Name 不是?
在此先感谢您的帮助!
编辑:忘记说明我在 web.config 中的内容(因为我确定如果我不这样做会被要求!)。
<system.web>
<customErrors mode="Off" />
<compilation debug="true" targetFramework="4.0" />
<authentication mode="Windows"/>
</system.web>
<system.webServer>
<httpProtocol>
<customHeaders>
<clear />
<add name="X-UA-Compatible" value="IE=10,chrome=1; IE=9,chrome=1; IE=8,chrome=1; IE=edge,chrome=1" />
</customHeaders>
</httpProtocol>
<modules runAllManagedModulesForAllRequests="true" />
<defaultDocument>
<files>
<add value="Home.aspx" />
</files>
</defaultDocument> …Run Code Online (Sandbox Code Playgroud)