我读过很多关于OWIN和Katana项目的文章,但我无法全面了解它.
对于使用ASP.NET的普通Web开发人员:
我让OwinStartup配置代码完美运行,然后它停止工作.不幸的是,我不确定我做了什么让它停止工作并且很难搞清楚它.
为了确保我已经涵盖了基础知识,我加倍检查以确保我拥有
[assembly:OwinStartup(typeof(WebApplication.Startup))]
Run Code Online (Sandbox Code Playgroud)
正确分配属性并确保我没有owin的appSetting:AutomaticAppStartup设置为false所以我将一个设置为true以保证安全,因为之前没有任何内容.
<add key="owin:AutomaticAppStartup" value="true" />
Run Code Online (Sandbox Code Playgroud)
我也尝试过专门调用appSetting:
<add key="owin:appStartup" value="WebApplication.Startup" />
Run Code Online (Sandbox Code Playgroud)
在它停止工作之前,我将Microsoft.Owin.Security NuGet包升级到2.0.2,所以我尝试将它们恢复到2.0.1(这很痛苦),但它没有改变任何东西.我在项目上安装了WebActivator并使用它来引导其他东西,但我已经在新的WebApplication模板上测试了它并且它在那里工作,所以我不认为那是罪魁祸首.
我还尝试删除我的Startup类并使用Visual Studio在Add New Item中使用OWIN Startup Class类型添加一个新类,并且也没有被调用.接下来我尝试添加第二个Startup类,因为我知道如果定义了多个OwinStartup属性它将抛出异常,但它没有抛出任何异常.
不知道还有什么可以尝试.有什么想法吗?
更新
事实证明Resharper在我用它删除未使用的引用时删除了对Microsoft.Owin.Host.SystemWeb的引用.
我是OWIN和Katana的新手.我真的不明白为什么我应该使用OWIN,而我可以使用IIS.为了简化,我的问题是:如果我跳过学习OWIN并在我的网站上使用IIS,我会失去什么?
我用谷歌搜索,但没有一个简单的解释.有一些信息,在这里,但他们使用的一些术语的短语,所以我无法理解这一点.
我收到此错误,因为我的项目无法找到OWIN启动类的参考.我甚至通过Nuget安装了所有OWIN参考包仍然遇到了同样的问题.我正在使用Visual Studio 2012和MVC4.
尝试加载应用程序时发生以下错误.
- 找不到包含OwinStartupAttribute的程序集.
- 找不到包含Startup或[AssemblyName] .Startup类的程序集.要禁用OWIN启动发现,请在web.config中添加值为"false"的appSetting owin:AutomaticAppStartup.要指定OWIN启动程序集,类或方法,请在web.config中添加appSetting owin:AppStartup以及完全限定的启动类或配置方法名称.
例如,在一个全新的ASP.NET MVC 5应用程序中,使用MVC w /个人帐户模板,如果我删除Global.asax.cs该类并将其配置代码移动到Startup.cs Configuration()方法如下,有什么缺点?
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
ConfigureAuth(app);
}
}
Run Code Online (Sandbox Code Playgroud)
对我来说,好处是,当将ASP.NET 4应用程序升级到ASP.NET 5并使用现在必须在Startup.cs类中配置的部分时,我不会在两个看起来相关的不同类中进行依赖注入和其他配置启动和配置.
如何配置我的mvc/webapi项目,以便从剃刀视图调用的webapi方法在未经授权时不返回登录页面?
它是一个MVC5应用程序,它也有通过javascript调用的WebApi控制器.
以下两种方法
[Route("api/home/LatestProblems")]
[HttpGet()]
public List<vmLatestProblems> LatestProblems()
{
// Something here
}
[Route("api/home/myLatestProblems")]
[HttpGet()]
[Authorize(Roles = "Member")]
public List<vmLatestProblems> mylatestproblems()
{
// Something there
}
Run Code Online (Sandbox Code Playgroud)
通过以下角度代码调用:
angular.module('appWorship').controller('latest',
['$scope', '$http', function ($scope,$http) {
var urlBase = baseurl + '/api/home/LatestProblems';
$http.get(urlBase).success(function (data) {
$scope.data = data;
}).error(function (data) {
console.log(data);
});
$http.get(baseurl + '/api/home/mylatestproblems')
.success(function (data) {
$scope.data2 = data;
}).error(function (data) {
console.log(data);
});
}]
);
Run Code Online (Sandbox Code Playgroud)
所以我没有登录,第一个方法成功返回数据.第二种方法返回(在成功函数中)包含等效登录页面的数据.也就是说,如果你请求一个标有[授权]并且你没有登录的控制器动作,你会在mvc中获得什么.
我希望它返回401未授权,以便我可以根据用户是否登录显示不同的数据.理想情况下,如果用户已登录,我希望能够访问Controller的用户属性,以便我可以返回特定于该成员的数据.
更新:由于下面的建议似乎都不再适用(对Identity或WebAPI的更改)我在github上创建了一个原始示例,它应该说明问题.
我在网上看到过很多类似的页面,但大多数都使用新项目而不是现有项目,或者没有必要的功能.所以,我有一个现有的MVC 5项目,并希望将ASP.NET MVC5 Identity与登录,电子邮件确认和密码重置功能集成.
除此之外,我还需要在数据库上创建所有必要的表,即用户,角色,组等(我在我的项目中使用EF Code First).是否有符合这些需求的文章或样本?任何建议将不胜感激.提前致谢...
使用Owin cookie身份验证时遇到一个奇怪的问题.
当我启动我的IIS服务器时,身份验证在IE/Firefox和Chrome上运行得非常好.
我开始使用身份验证进行一些测试并在不同的平台上登录,我想出了一个奇怪的错误.偶尔Owin框架/ IIS只是不向浏览器发送任何cookie.我将输入一个用户名和密码,该代码运行正确,但根本没有cookie传递给浏览器.如果我重新启动服务器它开始工作,那么在某些时候我将尝试登录并再次cookie停止交付.单步执行代码不会做任何事情并且不会抛出任何错误.
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationMode = AuthenticationMode.Active,
CookieHttpOnly = true,
AuthenticationType = "ABC",
LoginPath = new PathString("/Account/Login"),
CookiePath = "/",
CookieName = "ABC",
Provider = new CookieAuthenticationProvider
{
OnApplyRedirect = ctx =>
{
if (!IsAjaxRequest(ctx.Request))
{
ctx.Response.Redirect(ctx.RedirectUri);
}
}
}
});
Run Code Online (Sandbox Code Playgroud)
在我的登录程序中,我有以下代码:
IAuthenticationManager authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
authenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
var authentication = HttpContext.Current.GetOwinContext().Authentication;
var identity = new ClaimsIdentity("ABC");
identity.AddClaim(new Claim(ClaimTypes.Name, user.Username));
identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, user.User_ID.ToString()));
identity.AddClaim(new Claim(ClaimTypes.Role, role.myRole.ToString()));
authentication.AuthenticationResponseGrant =
new AuthenticationResponseGrant(identity, new AuthenticationProperties()
{
IsPersistent = isPersistent
}); …Run Code Online (Sandbox Code Playgroud) 我一直在寻找一个小时试图弄清楚为什么这不起作用.
我有一个带有WebAPI的ASP.Net MVC 5应用程序.我正在尝试获取Request.GetOwinContext().身份验证,但我似乎无法找到如何包含GetOwinContext.这是我的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using TaskPro.Models;
namespace TaskPro.Controllers.api
{
public class AccountController : ApiController
{
[HttpPost]
[AllowAnonymous]
public ReturnStatus Login(LoginViewModel model)
{
if (ModelState.IsValid)
{
var ctx = Request.GetOwinContext(); // <-- Can't find this
return ReturnStatus.ReturnStatusSuccess();
}
return base.ReturnStatusErrorsFromModelState(ModelState);
}
}
}
Run Code Online (Sandbox Code Playgroud)
从我读过的内容来看,它应该是System.Net.Http的一部分,但我已经包含了它,但它仍然没有解决.Ctrl-Space也没有给我任何intellisense选项.
我在这里错过了什么?
我使用Visual Studio 2013附带的Web Api 2模板有一些OWIN中间件来进行用户身份验证等.
在OAuthAuthorizationServerOptions我注意到,OAuth2服务器设置为分发在14天后到期的令牌
OAuthOptions = new OAuthAuthorizationServerOptions
{
TokenEndpointPath = new PathString("/api/token"),
Provider = new ApplicationOAuthProvider(PublicClientId,UserManagerFactory) ,
AuthorizeEndpointPath = new PathString("/api/Account/ExternalLogin"),
AccessTokenExpireTimeSpan = TimeSpan.FromDays(14),
AllowInsecureHttp = true
};
Run Code Online (Sandbox Code Playgroud)
这不适合我的最新项目.我想分发可以使用a刷新的短暂bearer_tokensrefresh_token
我做了很多谷歌搜索,找不到任何有用的东西.
所以这就是我设法得到的.我现在已经达到了"WTF do I now"的地步.
我写了一个根据类的属性RefreshTokenProvider实现:IAuthenticationTokenProviderRefreshTokenProviderOAuthAuthorizationServerOptions
public class SimpleRefreshTokenProvider : IAuthenticationTokenProvider
{
private static ConcurrentDictionary<string, AuthenticationTicket> _refreshTokens = new ConcurrentDictionary<string, AuthenticationTicket>();
public async Task CreateAsync(AuthenticationTokenCreateContext context)
{
var guid = Guid.NewGuid().ToString();
_refreshTokens.TryAdd(guid, context.Ticket);
// hash??
context.SetToken(guid);
}
public async Task …Run Code Online (Sandbox Code Playgroud)