标签: owin

app.UseOAuthBearerTokens与ASP.NET Identity 2.0的DbContext中间件?

编辑:进展后,我可以缩小问题的范围:

应该对VS2013 SPA模板中的startup.auth.cs和ApplicationOAuthProvider.cs进行哪些更改(使用ASP.NET标识1.0),以便将其迁移到使用ASP.NET标识2.0?

编辑2:我进一步简化了这个问题.如何将app.UseOAuthBearerTokens与ASP.NET Identity 2.0的中间件一起用于检索DbContext?

        app.UseOAuthBearerTokens(new Microsoft.Owin.Security.OAuth.OAuthAuthorizationServerOptions()
            {
                //What goes here??
            });
Run Code Online (Sandbox Code Playgroud)

(样本中没有这样的例子.)

与Asp.net身份框架的V1.0到V2.0alpha存在显着差异.有一个示例可以显示如何使用V2:

https://aspnet.codeplex.com/SourceControl/latest (参见Samples-> Identity-> ChangePK)

但那个例子不是MVC或SPA.话虽这么说,我有一个从VS2013 ASP.NET SPA应用程序(其中包含Identity 1.0)构建的应用程序.我一直在尝试在我的MVC应用程序中的示例中实现代码,但我不清楚VS2013 SPA模板中的哪些代码被删除,以支持示例中的代码.

问另一种方式,是否有人有指导在ASP.NET MVC应用程序中实现ASP.NET identity 2.0 alpha?(理想情况下,从利用身份1.0的VS2013 MVC SPA模板迁移的步骤)

asp.net-mvc entity-framework owin visual-studio-2013 asp.net-identity

2
推荐指数
1
解决办法
1万
查看次数

如何在没有aspnet标识的情况下使用OWIN表单身份验证

我想使用OWIN管道向客户端发出身份验证cookie,当用户注销时清除cookie但我不想使用恼人的aspnet身份提供者.它只是不适合项目,我试了很长时间,它只是不适合.所以我决定不使用它.我只想使用OWIN管道.

这是可能的还是我应该回到旧的表单身份验证?

forms asp.net asp.net-mvc forms-authentication owin

2
推荐指数
1
解决办法
3765
查看次数

如何使用ASP.Net MVC5和OWIN返回StatusCode 401

我想知道如何使用ASP.Net MVC5,我的IExceptionFilter,Microsoft.Owin和ui-router(https://github.com/angular-ui/ui-router)的实现.

我的情况是:当权限出现问题时,我会使用以下内容向客户端发送响应:

    public void OnException(ExceptionContext filterContext)
    {
        if (filterContext.Exception == null)
        {
            return;
        }

        ...

        filterContext.HttpContext.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
        filterContext.Result = "Unathorized access";
    }
Run Code Online (Sandbox Code Playgroud)

请注意:我已将StatusCode代码设置为401.

要从服务器获取视图模板,我使用$ stateProvide请求Action方法.如您所知,$ stateProvide是ui-router的核心部分,可以处理$ stateChangeError.不幸的是,不是我的情况.

我需要使用Microsoft.Owin.由于某些原因,我的响应有StatusCode = 200(不是401)并且具有"X-Responded-JSON"标头:

X-Responded-JSON:{"status":401,"headers":{"location":"<URL>"}}
Run Code Online (Sandbox Code Playgroud)

以下是来自http://kevin-junghans.blogspot.de/2013/12/returning-401-http-status-code-on.html的引用"... OWIN和MVC 5中的安全管道已经改变,自定义属性不再返回401和403状态代码.而是返回200状态代码并在标题中插入一些其他信息......"

基本上,这意味着不会触发$ stateChangeError.

有人知道如何解决这个问题吗?谢谢.

asp.net-mvc angularjs owin asp.net-mvc-5 angular-ui-router

2
推荐指数
1
解决办法
1442
查看次数

为什么在HttpRequest结束后第二次创建了owin中间件

根据为什么每个请求创建和处理来自Asp.Net Identity 的问题,ApplicationDbContext我做了一些研究,为什么会发生这种情况.我发现实际ApplicationDbContext创建了一次,HttpRequest但是当使用Owin管道时,Owin Middleware将在HttpRequest结束后第二次创建.

因此,ApplicationDbContext当用户点击一个链接时,确实创建了第二次,给出的印象是每次创建对象两次WebRequest.

经过大量的研究,我决定在不使用任何身份验证的情况下启动一个简单的MVC 5项目.从NuGet添加Owin中间件后,我创建了以下Owin Middleware组件.这基本上检查HttpContext字典中是否存在某些假对象,并在字典中不存在时创建一个假对象.输出将写入调试窗口以简化操作.

[assembly: OwinStartupAttribute(typeof(MvcPlain.Startup))]
namespace MvcPlain
{
    public class Startup {
        public static int Counter;
        public static string FakeKeyName;

        public void Configuration(IAppBuilder app) {
            app.Use(async (context, next) =>
            {
                Debug.WriteLine("Owin middleware entered => begin request");

                FakeKeyName = "owinKey" + Counter.ToString();
                var fakeKeyPresent = HttpContext.Current.Items.Contains(FakeKeyName);

                Debug.WriteLine(string.Format("{0} key present in HttpContext?: {1}", 
                                                        FakeKeyName, fakeKeyPresent));

                if (!HttpContext.Current.Items.Contains(FakeKeyName))
                {
                    Counter += 1; …
Run Code Online (Sandbox Code Playgroud)

c# owin asp.net-mvc-5 owin-middleware

2
推荐指数
1
解决办法
1987
查看次数

AuthenticationProperties.RedirectUri未在Challenge()中传递给Google

在我的网络应用程序中,我已将Google注册为单点登录提供商:

app.UseGoogleAuthentication(new GoogleOAuth2AuthenticationOptions {
    ClientId = "8765.......apps.googleusercontent.com",
    ClientSecret = "Secret"
})
Run Code Online (Sandbox Code Playgroud)

我的应用不允许用户注册/注册(而是他们的帐户由管理员创建,但他们稍后可以将他们的帐户与Google关联).

在我的"使用Google登录"控制器中,我正在尝试发布一个Challenge()重定向到Google.这可能不是正确的方法:

string redirectUri = "http://localhost:55262/SSO/Google/ProcessToken"; // actually created in code, but shown as string for clarity
AuthenticationProperties properties = new AuthenticationProperties();
properties.RedirectUri = Server.UrlEncode(redirectUri);
Context.GetOwinContext().Authentication.Challenge(properties, "Google");
Run Code Online (Sandbox Code Playgroud)

这正确地将用户发送给Google,但Google会出现错误:redirect_uri_mismatch,说:

请求中的重定向URI:http:// localhost:55262/signin-google 与注册的重定向URI不匹配.

我在Google控制面板中的返回URI集合不包含redirect_uri指定之前看到过此错误.

如果我在VS2015中调试,我可以看到redirect_uri属性设置正确AuthenticationProperties,但似乎OWIN/Katana没有将它传递给Google.相反,当我点击Google时,return_uri是OWIN/Katana使用的默认值.我设置的那个被忽略了.

Google请求详细信息似乎证实了这一点:

scope=openid profile email
response_type=code
redirect_uri=http://localhost:55262/signin-google
Run Code Online (Sandbox Code Playgroud)

我在这做错了什么?我是否应该Challenge()不允许用户将其本地应用程序帐户与Google相关联?

asp.net asp.net-mvc owin katana google-signin

2
推荐指数
2
解决办法
5048
查看次数

使用Asp.net 4.0实施OAuth2

我有一个.NET 4.0应用程序,我需要添加一个第三层的OAuth2身份验证。我有点困惑(很难找到.NET 4.0的示例和文档)。

我可以将Microsoft.AspNet.Membership.OpenAuth(OAuth 2.0)与Asp.net 4.0(.NET 4.0)一起使用吗?

我的另一个选择是使用DotNetOpenAuth,但是在.NET 4.0中找到带有Webforms回调的示例时遇到了一些麻烦。

据我了解,我应该有一个身份验证页面(登录页面):

var medOK = new WebServerClient(GetAuthServerDescription(), clientIdentifier: "some client id");
medOK.ClientCredentialApplicator = ClientCredentialApplicator.PostParameter("some secret code");

// CallBack
var state = new AuthorizationState();
var uri = Request.Url.AbsoluteUri;
uri = RemoveQueryStringFromUri(uri);
state.Callback = new Uri(uri); 
var accessTokenResponse = medOK.ProcessUserAuthorization();
if (accessTokenResponse != null){
    //If you have accesstoek then do something 
} else if (this.AccessToken == null) {
    // If we don't yet have access, immediately request it.
    medOK.PrepareRequestUserAuthorization(state);
    medOK.RequestUserAuthorization();
}
Run Code Online (Sandbox Code Playgroud)

还有一个回调页面(例如ashx页面):

var medOK …
Run Code Online (Sandbox Code Playgroud)

c# .net-4.0 dotnetopenauth oauth-2.0 owin

2
推荐指数
1
解决办法
6842
查看次数

使用owin和ninject在webapi应用程序中的无参数构造函数问题

我有一个web api应用程序,我想使用Owin,OauthNinject,所以我有这个配置

依赖注入器

public class NinjectDependencyResolver : IDependencyResolver
{
    private static IKernel kernel; 

    public NinjectDependencyResolver()
    {
        kernel = new StandardKernel();
       // AddBindings();
    }

    public object GetService(Type serviceType)
    {
        return kernel.TryGet(serviceType);
    }

    public IEnumerable<object> GetServices(Type serviceType)
    {
        return kernel.GetAll(serviceType);
    }

    private static void AddBindings()
    {
        kernel.Bind<INotifier>().To<Notifier>();
        kernel.Bind<IEventRepository>().To<EventRepository>();
        kernel.Bind<ICrud<Config>>().To<CrudConfig>();
        kernel.Bind<ICrud<Evenement>>().To<CrudEvent>();
        kernel.Bind<IAccount>().To<Account>();
    }



    public static Lazy<IKernel> CreateKernel = new Lazy<IKernel>(() =>
    {
        //var kernel = new StandardKernel();
        kernel.Load(Assembly.GetExecutingAssembly());
        kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
        kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
        AddBindings();

        return …
Run Code Online (Sandbox Code Playgroud)

.net c# ninject asp.net-web-api owin

2
推荐指数
1
解决办法
1863
查看次数

我可以访问OWIN环境字典吗?

我可以在ASP.NET Core中访问OWIN环境字典吗?

我想从Startup.cs文件中访问它.

我想访问它的原因是因为我想将OWIN环境字典传递给我的中间件或从我的中间件中访问它.

owin asp.net-core

2
推荐指数
1
解决办法
2388
查看次数

在asp mvc的上下文中找不到owin.Environment项

这是我在身份中注册的代码:

[AllowAnonymous]
public ActionResult Register()
{
    var roles = db.Roles.Select(r => new { RoleID = r.Id, RoleName = r.Name }).ToList();
    ViewBag.Roles = new SelectList(roles, "RoleID", "RoleName");

    return View();
}

//
// POST: /Account/Register
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegisterViewModel model, HttpPostedFileBase UserPhoto)
{
    if (ModelState.IsValid)
    {
        model.DateRegister = DateTime.Now;
        var user = new ApplicationUser
        {
            UserName = model.UserName,
            Name = model.Name,
            Family = model.Family,
            PhoneNumber = model.PhoneNumber,
            Gender = model.Gender,
            BirthDay = model.BirthDay,
            DateRegister = model.DateRegister,
            IsActive = false, …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-mvc owin katana asp.net-identity-2

2
推荐指数
1
解决办法
4012
查看次数

在.NET标准中创建OWIN中间件

TL; DR;

有没有一种方法可以在.NET Standard中创建与Microsoft.Owin向后兼容的Owin中间件?

细节

我创建了一个简单的中间件,例如:

public class RequestTrackingMiddleware
{
    private readonly RequestDelegate _next;

    public RequestTrackingMiddleware(RequestDelegate next)
    {
        _next = next;
    }

    public async Task Invoke(HttpContext context)
    {
        var sw = Stopwatch.StartNew();
        await _next(context);
        sw.Stop();
        Console.WriteLine(sw);
    }
Run Code Online (Sandbox Code Playgroud)

我将它与Owin一起添加到“经典” ASP.NET应用程序中。使用方法:

public override void Configuration(IAppBuilder app)
{
    app.Use<FinaiRequestTrackingMiddleware>();
}
Run Code Online (Sandbox Code Playgroud)

但我最终得到:

Microsoft.AspNet.Identity.Owin.IdentityFactoryMiddleware`2 [....]和Microsoft.AspNetCore.Http.RequestDelegate之间没有可用的转换。

有任何想法吗?

asp.net owin asp.net-core

2
推荐指数
1
解决办法
2163
查看次数